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.
1 10000 4 2 4000 400 3000 250
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 10005 13 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背包要复杂一点儿,是完全背包问题.
完全背包
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背包的状态转移方程.
JXUST第二赛-C. Investment,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/mercu/p/3858039.html