標籤:des style blog color os 資料
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.
InputThe 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.OutputFor 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: [email protected] 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背包的狀態轉移方程.