Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2844
The meaning of this question is only after reading it for a long time.
N and m respectively represent n silver coins. the purchased items are no higher than m yuan.
Then we will give you 2 * n pieces of data. The first n pieces are values, and the last n pieces are numbers.
Then I asked you, how many groups of coins can be combined not greater than m?
That is, there are several situations of 1 to M.
There are two cases for this question: 1. There are n items available for items. 2. There is a maximum number of items available.
So we are considering using multiple backpacks.
The binary Optimization of multiple backpacks should be considered here. Otherwise, the three loops will time out.
So the most important thing is the dynamic equation. How can we list the dynamic equation for this question?
We will analyze it as follows:
Our goal is to store money values in DP so that we can calculate values larger than M.
So the value of money is stored in DP. What about the subscript of DP?
Of course, it is the value of coins.
Because:
Every update of a coin will surely have its own value.
For example, if it is 1 or 2, we certainly cannot change this value (you can't give you 2 Yuan coin, do you calculate 1 yuan ?)
So every update, we only need to use the coin value as the subscript.
Then we can list the dynamic equations:
DP [I] = max (DP [I], DP [I-value] + weight); // only the Template
The AC code is as follows:
# Include <iostream> # include <stdio. h> using namespace STD; int DP [100010]; // note the array size int value [2000], num [2000]; int n, m; int max (int, int B) {If (A> B) return a; elsereturn B;} void zeroonepack (INT value, int weight) {int I; for (I = m; i> = value; I --) DP [I] = max (DP [I], DP [I-value] + weight);} void completepack (INT value, int weight) {int I; for (I = value; I <= m; I ++) DP [I] = max (DP [I], DP [I-value] + weight);} void multiplepack (INT value, int weight, int num) {If (value * num> = m) // convert to a full backpack completepack (value, weight); else // binary optimization {int K = 1; while (k <= num) {zeroonepack (K * value, K * weight); num-= K; k <= 1 ;}zeroonepack (Num * value, num * value) ;}} int main () {While (scanf ("% d", & N, & M) {If (n + M = 0) break; int I; for (I = 1; I <= m; I ++) DP [I] =-10000000; // copy to the smallest DP [0] = 0; for (I = 0; I <N; I ++) scanf ("% d", & value [I]); for (I = 0; I <n; I ++) scanf ("% d ", & num [I]); for (I = 0; I <n; I ++) multiplepack (value [I], value [I], num [I]); // multiple backpacks int ans = 0; for (I = 1; I <= m; I ++) // statistics {If (DP [I]> 0) ans ++;} printf ("% d \ n", ANS);} return 0 ;}
I have recently started to update my blog, and I hope you will give me some comments and add your personal opinions ~