Question: How many kinds of money can be formed with a certain amount of paper money and a nominal value without exceeding the total amount of money?
Thought: I thought of multiple backpacks. I wrote it with multiple backpacks, tle. Later I used binary optimization or tle. Later I checked the dis and said I would use the priority queue to search for information online, did not understand, later look at acmj1991 blog, said that the use of array marking, and then do not understand Shenma is the array marking method. Later I learned that it is actually a conventional method, but it is very powerful. Specifically, for each item, the amount of circulating money ranges from its own value to the total value. It is determined at each cycle. If this value does not appear, ans adds 1. flag [I] indicates whether the value I exists. num [I] indicates the value of the current item, and sum indicates the quantity of the current item, then if (flag [I-num [I] & cnt [I-num [I] <sum &&! When the flag [I] is created, ans adds 1. Cnt [I] indicates the current number of items used when the value of the current item is I.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Const int n= 100010;
Int num [110];
Bool flag [N];
Int cnt [N];
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int n, totalvalue;
While (scanf ("% d", & n, & totalvalue) & n & totalvalue ){
For (int I = 0; I <n; ++ I)
Scanf ("% d", & num [I]);
CLR (flag, false );
Flag [0] = true;
Int sum = 0, ans = 0;
For (int I = 0; I <n; ++ I ){
Scanf ("% d", & sum );
CLR (cnt, 0 );
For (int j = num [I]; j <= totalvalue; ++ j ){
If (flag [j-num [I] & cnt [j-num [I] <sum &&! Flag [j]) {
Flag [j] = true;
Ans ++;
Cnt [j] = cnt [j-num [I] + 1;
}
}
}
Printf ("% d \ n", ans );
}
Return 0;
}
Author: wmn_wmn