Cash machine
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:26954 |
|
Accepted:9533 |
Question: There are various currencies with different denominations, each of which has different quantities of currencies. Please find the amount of cash that can be used to make up the nearest and less than or equal to the given number. Multiple backpacks turn 0 1 backpack The possible status of currency I is W [I] to cash. State EquationDP [J] = DP [J-C [I] + W [I] (C [I] indicates that "volume" is actually equivalent to W [I] for easy differentiation) Use count [I] to record the usage of the currency in N [I]. Note that before each entry into the inner loop (that is, clear count 0 before currency change)
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int main () {int cash, num, I, j; int N [15]; int W [15]; int DP [10001]; int count [10001]; while (CIN> cash> num) {for (I = 0; I <num; I ++) CIN> N [I]> W [I]; memset (DP, 0, sizeof DP ); for (I = 0; I <num; I ++) {memset (count, 0, sizeof count); For (j = W [I]; j <= cash; j ++) if (DP [J] <DP [J-W [I] + W [I] & COUNT [J-W [I] <n [I]) {DP [J] = DP [J-W [I] + W [I]; count [J] = count [J-W [I] + 1 ;}} cout <DP [Cash] <Endl;} return 0 ;}
Poj 1276 cash machine (multiple backpacks)