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));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 著名财经杂志《财富》对本书的评论是:如果你一生只读一本关于投资的著作,无疑就是这本《聪明的投资者》。 首先介绍一下...
    惜她阅读 6,975评论 0 34
  • Less is more.(“少即是多”) 在过去物质匮乏的年代,不断做物质加法——为家里添置冰箱,买回电视机,配...
    王英的简书阅读 335评论 0 0
  • 张爱玲说,出名要趁早,来得太晚的话,快乐也不那么痛快。 其实,吃亏也要趁早,别等到太晚,连重来的机会都没有。 1 ...
    阿春牧羊犬阅读 6,694评论 49 122
  • 那一年你是否会选择我,青涩懵懂的我第一次出现在你的面前显得那么的腼腆。 "诶,同学,请问新生报到处在哪里啊?"陈莫...
    fd6db1c70418阅读 450评论 1 1