首页 > 代码库 > JXUST第二赛-C. Investment

JXUST第二赛-C. Investment

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: 

ValueAnnual
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

110000 424000 4003000 250

Sample Output

14050

 1 /************************************************************************* 2     > File Name: beibao.cpp 3     > Author: Mercu 4     > Mail: bkackback1993@gmail.com 5     > Created Time: 2014年07月20日 星期日 12时13分54秒 6  ************************************************************************/ 7  8 #include<iostream> 9 #include<string.h>10 using namespace std;11 12 #define MAXN 1000513 14 int w[MAXN],v[MAXN];15 int f[200000];16 17 int main()18 {19     int a,b,c,d,g,k,j,i;20     cin>>a;21     while(a--)22     {23         memset(w,0,sizeof(w));24         memset(v,0,sizeof(v));25         memset(f,0,sizeof(f));26         cin>>b>>c;27         cin>>d;28         for(i = 0;i < d;i++)29         {30             cin>>w[i]>>v[i];31             w[i] /= 1000;32         }33         int m = b;34         for(k = 0;k < c;k++)35         {36             m = b;37             m /= 1000;38             for(i = 0;i < d;i++)39             {40                 for(j = w[i];j <= m;j++)41                 {42                     f[j] = f[j]>(f[j-w[i]] + v[i])?f[j]:(f[j-w[i]] + v[i]);43                 }44             }45             b += f[m];46         }47         cout<<b<<endl;48     }49     return 0;50 }

   

  这个问题比0-1背包要复杂一点儿,是完全背包问题.

  

  有N种物品和一个容量为V的背包,每种物品都有无限件可用。
  第i种物品的体积是c,价值是w。求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.
                                      --from 某度百科.
  意思大概就这样儿.因为里面说The value of a bond is always a multiple of $1 000,为了减小数据,我们可以把最大价值和每种债券需要投入的本都除以1000,这样数据便会小很多.
  把原来的本金记录下来,再加上利息就是我们所求.而最大的利息就可以通过完全背包来实现.
 完全背包
for(k = 0;k < c;k++) //年份 2 { 3 m = b; 4 m /= 1000; 5 for(i = 0;i < d;i++) 6 { 7 for(j = w[i];j <= m;j++) 8 { 9 f[j] = f[j]>(f[j-w[i]] + v[i])?f[j]:(f[j-w[i]] + v[i]);10 }11 }12 b += f[m];13 }
        cout<<b<<endl;

 这段就是核心代码,c表示买的年份,d表示债券的种类有多少种,m表示投入本金除以1000,w[i]就是每种债券需要投入的本金,第一个for是多少年.

  5-12行是一个完全背包求解过程,5行表示种类,0-d.7行表示金额,w[i]到m.

0-1背包
for(i = 1;i <= a;i++)2 {3 for(j = b;j >= t[i];j--)4 {5 if(t[i] <= j)6 f[j] = max(f[j],f[j - t[i]] + v[i]);7 }8 }9 cout<<f[b]<<endl;

这里要和0-1背包区别.0-1背包是m--w[i],而完全背包是w[i]--m,相反,其次,状态转移方程也有区别.

1 f[j]=max{f[j],f[j-k*c]+k*w}(0<=k*c<=v)2 f[j] = f[j]>(f[j-w[i]] + v[i])?f[j]:(f[j-w[i]] + v[i]); 
上面就是完全背包的状态转移方程,
1 f[j] = max(f[j],f[j - t[i]] + v[i]);2 f[i, j] = max( f[i-1, j-Wi] + Pi (j >= Wi), f[i-1, j] )

 

  上面就是0-1背包的状态转移方程.