Poj 2063 Investment

題目

Poj 2063 Investment

Description
John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor. 
John did not need that much money for the moment. But he realized that it would be a good idea to store this capital in a safe place, and have it grow until he decided to retire. The bank convinced him that a certain kind of bond was interesting for him. 
This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has no fixed term. Bonds are available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy was not trivial to figure out. Moreover, after a few years his capital would have grown, and the schedule had to be re-evaluated. 
Assume the following bonds are available: 
Value   Annual
interest
4000
3000    400
250

With a capital of e10 000 one could buy two bonds of $4 000, giving a yearly interest of $800. Buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. After two years the capital has grown to $11 800, and it makes sense to sell a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. This is where this story grows unlikely: the bank does not charge for buying and selling bonds. Next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200. 
Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.

Input
The first line contains a single positive integer N which is the number of test cases. The test cases follow. 
The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000), and the number of years the capital may grow (at most 40). 
The following line contains a single number: the number d (1 <= d <= 10) of available bonds. 
The next d lines each contain the description of a bond. The description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $1 000. The interest of a bond is never more than 10% of its value.
Output

For each test case, output – on a separate line – the capital at the end of the period, after an optimal schedule of buying and selling.

Sample Input
1
10000 4
2
4000 400
3000 250

Sample Output
14050

具體的內容就是一共有多組債券,分別給出了每組債券的價格與每年的利息,然后根據投資的年數與投資的初始錢數,計算最終的總價值。
輸入:
N(測試數據數量)
amount(初始錢數) years(年數)
d(債券數量)
value(債券價格) interest(每年的利息)
...(一共 d 組)

思路

核心其實還是背包問題,關鍵有幾個點:

  1. 初始錢數最大為 1000000,年數最大為 40,利息最大為 10%,有此我們可以計算出最終的結果最大約為 45260000,按照背包的思路,如果直接開這么大的數據內存就直接爆掉了,而題目給了每組債券都是 1000 的整數倍,所以 1000 以下的部分其實跟利息的計算是毫無關系的(僅僅是每年喲),而 1000 以上的部分可以直接通過除掉 1000 來減小數組的大小,這樣數組其實開到 45260 就可以了。
  2. 先暴力計算出所有可能金錢得到的利息數(即 result[]),這樣在以后每年計算的時候都可以直接取值就可以了
  3. 剩下的問題就是計算 result[] 了,標準的背包問題,這里我就不細說了,如果不了解的話直接看《背包九講》吧。

代碼

// http://poj.org/problem?id=2063
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include<math.h>
using namespace std;

#define max(x,y) (x > y ? x : y)

//債券
struct Bond {
    // 債券的價格
    int value;
    
    // 債券的利息
    int interest;
};

// 債券的總列表
Bond bondList[15];

// index 代表金額(除以 1000 后的值),result[index] 為這個金額最大的利息數
int result[50000];

/**
 * 根據債券的信息,更新 result 數據
 **/
void updateResult(int maxAmount, int boudNum) {
    for (int i = 0; i < boudNum; i++) {
        int value = bondList[i].value;
        int interest = bondList[i].interest;
        
        int lastMax = 0;
        for(int t = 0; t <= maxAmount - value; t++) {
            if (result[t] != 0) {
                lastMax = result[t];
            }
            result[t + value] = max(result[t + value], result[t] + interest);
            if (result[t] == 0) {
                result[t] = lastMax;
            }
        }
    }
}

/**
 * 根據 updateResult 計算出來的結果,計算最終結果
 **/
int calculate(int amount, int years) {
    int total = amount;
    for(int i = 0; i < years; i++) {
        total += result[total / 1000];
    }
    return total;
}

int main(int argc, const char * argv[]) {
    
    // 處理輸入數據,不多解釋
    int n, amount, years, boudNum;
    scanf("%d", &n);
    
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &amount, &years, &boudNum);
        for (int t = 0; t < boudNum; t++) {
            scanf("%d %d", &bondList[t].value, &bondList[t].interest);
            // 因為債券的價格都是 1000 的整數倍,所以這里可以除掉 1000
            bondList[t].value /= 1000;
        }

        // 初始化數據,避免每個 testcase 互相影響
        memset(result, 0, sizeof(result));
        
        // 復利,每年最高 10%,可以計算出最大的可能結果,剪枝,避免過多的計算
        int maxAmount = ((int)(pow(1.1, years) * amount / 1000) + 1);
        
        updateResult(maxAmount, boudNum);
        printf("%d\n", calculate(amount, years));
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,001評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,786評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,986評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,204評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,964評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,354評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,410評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,554評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,106評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,918評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,093評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,648評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,342評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,755評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,009評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,839評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,107評論 2 375

推薦閱讀更多精彩內容

  • 著名財經雜志《財富》對本書的評論是:如果你一生只讀一本關于投資的著作,無疑就是這本《聰明的投資者》。 首先介紹一下...
    惜她閱讀 6,905評論 0 34
  • Less is more.(“少即是多”) 在過去物質匱乏的年代,不斷做物質加法——為家里添置冰箱,買回電視機,配...
    王英的簡書閱讀 333評論 0 0
  • 張愛玲說,出名要趁早,來得太晚的話,快樂也不那么痛快。 其實,吃虧也要趁早,別等到太晚,連重來的機會都沒有。 1 ...
    阿春牧羊犬閱讀 6,685評論 49 122
  • 那一年你是否會選擇我,青澀懵懂的我第一次出現在你的面前顯得那么的靦腆。 "誒,同學,請問新生報到處在哪里啊?"陳莫...
    fd6db1c70418閱讀 449評論 1 1