Description
The Byteotian Bit Bank has an advanced monetary system with a total of n denominations of coins, B1, B2,..., bn. But each coin has a quantity limit, and now we want to make up the face value K for the minimum number of coins.
Input
The first row is a number n, 1 <= n <= 200. Next line n integers b1, b2,..., bn, 1 <= b1 < B2 < ... < b N <= 20 000, third row n integers c1, C2,..., cn, 1 <= ci <= 20 000, indicating the number of each coin. The last line of a number K – represents the number of denominations to be pooled, 1 <= k <= 20 000.
Output
A number in the first row indicates the minimum number of coins to be paid
Sample Input3
2 3 5
2 2 1
10
Sample Output3Analysis: Very typical multi-pack, regardless of other, F[i][j] said in the first I kind of coins, to get to J-yuan minimum number of coins required. The dynamic transfer equation is: F[i][j]=min (F[i-1][j-x[i]]+1,f[i-1][j]), (X[i] is the value of the current coin) (of course we can use dynamic array to optimize to one-dimensional);so just convert it completely into a 01 backpack. But will find in this topic directly into 01 will time out, so you have to use the binary to optimize! As we all know, the sum of the power of 2 can be expressed as any number under the power, such as 2 of the positive numbers below the 5, can be represented by any combination of 1,2,4,8,16. Then for the number of coins as long as it is divided into 2 of the sum of power, you can form all possibilities. For example 18 can be divided into 1,2,4,8, plus not 2 of the power of 3, you can make up less than 18 of all natural numbers, enumerate all the possibilities, then the original 18 choices into 1,2,4,8,3 five choices, this is the value of five choices is different. By doing so will greatly reduce the 01 backpack. Well, look at the program.
#include <iostream>#include<cstdio>intnum,n,k,now,t,b[40000],x[40000],y[40000],a[4000005],f[4000005];using namespacestd;intMinintXinty) { if(x>y)returny; returnx;}intMain () {CIN>>N; Num=0; for(intI=1; i<=n;i++) cin>>A[i]; for(intI=1; i<=n;i++) {cin>>b[i];//Enter the number of such coinst=1;//several powers of enumeration 2 while(t*2-1<b[i])//If so far the sum of all 2 of the powers of the enumeration is less than the total number of coins{num+=1;//Array LengthX[num]=t*a[i];//We'll turn the enumerated T-item into a piece, x store the T-piece original item, which is now the value of this item.y[num]=t;//The number of coins represented by this itemt=t*2;//to enumerate the power of another 2.} num+=1; Y[num]=b[i]-(t1);//A few more coins are counted as one, as in the example of 3. X[num]=y[num]*a[i];//the value of the remaining item} cin>>K; //for (int i=1;i<=num;i++) cout<<x[i]<< ' <<y[i]<<endl; for(intI=0; i<=k;i++) f[i]=1000000005; f[0]=0; for(intI=1; i<=num;i++) for(intj=k;j>=x[i];j--)//pay attention to write downF[j]=min (F[j-x[i]]+y[i],f[j]);//best Dynamic array dimensionality reductioncout<<f[k]<<Endl; return 0;}
Well, it's done.
1531: [Poi2005]bank Notes binary optimization (c + +)