ZOJ 2224 Investment (完全背包)

水深无声 2024-02-17 21:26 48阅读 0赞

Investment


Time Limit: 10 Seconds Memory Limit: 32768 KB


John never knew he had a grand-uncle, until he received the notary��s letter. He learned thathis late grand-uncle had gathered a lot of money, somewhere in South-America, and thatJohn was the only inheritor.

John did not need that much money for the moment. But he realized that it would be agood idea to store this capital in a safe place, and have it grow until he decided to retire. Thebank 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, payedto the owner at the end of each year. The bond has no fixed term. Bonds are available indifferent sizes. The larger ones usually give a better interest. Soon John realized that theoptimal set of bonds to buy was not trivial to figure out. Moreover, after a few years hiscapital would have grown, and the schedule had to be re-evaluated.

Assume the following bonds are available:
















Value Annual interest
4000 400
3000 250

With a capital of 10000 one could buy two bonds of 4000, giving a yearly interest of800. Buying two bonds of 3000, and one of 4000 is a better idea, as it gives a yearlyinterest of 900. After two years the capital has grown to 11800, and it makes sense to sella 3000 one and buy a 4000 one, so the annual interest grows to 1050. This is where thisstory grows unlikely: the bank does not charge for buying and selling bonds. Next year thetotal sum is 12850, which allows for three times 4000, giving a yearly interest of 1200.

Here is your problem: given an amount to begin with, a number of years, and a set ofbonds with their values and interests, find out how big the amount may grow in the givenperiod, 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 testcases follow.

The first line of a test case contains two positive integers: the amount to start with (atmost 1000000), 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 availablebonds.

The next d lines each contain the description of a bond. The description of a bond consistsof two positive integers: the value of the bond, and the yearly interest for that bond. Thevalue of a bond is always a multiple of 1000. The interest of a bond is never more than10% of its value.

Output

For each test case, output �C on a separate line �C the capital at the end of the period, after anoptimal schedule of buying and selling.

Sample Input

1
10000 4
2
4000 400
3000 250

Sample Output

14050

分析:完全背包问题,刚开始数组开为100w,结果seg了,然后开为1000w,结果ME了,然后看了讨论说,可以吧数组压缩,因为债券值是1000的整数倍,所以所拥有的本息和 的零头小于1000 不可能买到债券,所以,将本息和 与 债券值同时除以1000对正确结果没影响,这样就达到了优化数组。

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. #define INF 10000000
  6. const int maxn=1e6+10;
  7. int dp[maxn];
  8. int bond[12],interes[12];
  9. int main() {
  10. int T;
  11. scanf("%d",&T);
  12. while(T--){
  13. int start,year;
  14. scanf("%d%d",&start,&year);
  15. int d;
  16. scanf("%d",&d);
  17. for(int i=0;i<d;i++){
  18. scanf("%d%d",&bond[i],&interes[i]);
  19. bond[i]/=1000;
  20. }
  21. int money=start;
  22. while(year--){
  23. int t=money/1000;
  24. for(int i=1;i<=t;i++)
  25. dp[i]=-INF;
  26. dp[0]=0;
  27. for(int i=1;i<=t;i++){
  28. for(int j=0;j<d;j++){
  29. if(i>=bond[j])
  30. dp[i]=max(dp[i],dp[i-bond[j]]+interes[j]);
  31. }
  32. }
  33. int cnt=0;
  34. for(int i=t;i>=1;i--)
  35. if(cnt<dp[i]){
  36. cnt=dp[i];
  37. }
  38. money+=cnt;
  39. }
  40. printf("%d\n",money);
  41. }
  42. return 0;
  43. }

发表评论

表情:
评论列表 (有 0 条评论,48人围观)

还没有评论,来说两句吧...

相关阅读

    相关 背包DP | 完全背包问题

    > 完全背包问题:有n种物品,每一件的物品重量为 w\[ i \],价值为 c\[ i \]。现有一个容量为V的背包 (背包的最大承重为V),问如何选取物品放入背包,使得背包内

    相关 完全背包

    完全背包在背包九讲中有很详细的讲解,但是今天碰到题尝试了一下他给的算法,发现并不快,看了一下其他的代码,速度很快! 题目链接http://hihocoder.com/prob

    相关 01背包,完全背包

    01背包问题:一个背包总容量为V,现在有N个物品,第i个 物品体积为weight\[i\],价值为value\[i\],现在往背包里面装东西,怎么装能使背包的内物品价值最大?

    相关 完全背包

    完全背包和01背包的区别 > 01背包:有n个物品,每种物品只能被使用一次 > > 完全背包:有n个物品,每个物品可以被多次使用 完全背包的递推公式可以由01背包的递推公

    相关 完全背包

    问题: 有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的费用是c\[i\],价值是w\[i\]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量

    相关 0-1背包&完全背包

    First:0-1背包问题 1.定义define: 所谓的0-1背包就是指每种物品只有一件,而每件物品只有两种选择,即选择放或是不放 2.问题: 一个小偷来出来活动