Question: The dining card in the canteen of the E-Science and Technology Department has a very strange design, that is, the balance is determined before the purchase. If, before purchasing a product,
If the remaining amount on the card is greater than or equal to 5 yuan, the purchase will be successful (even if the balance on the card is negative after purchase ),
Otherwise, you cannot purchase the Service (even if the amount is sufficient ). Therefore, we all hope to minimize the balance on the card. One day, N kinds of vegetables are sold in the canteen,
You can purchase each dish once. If you know the price of each dish and the balance on the card, ask the minimum amount of balance on the card.
Analysis: DP, 01 backpack. Enumeration of the last dish sold, the other 01 backpack to the maximum value of the M-5 can be.
T (n) = 1000 ^ 3 If a backpack is used directly;
Transfer the enumerated status from 1-to 1-50 (only 50 prices), and then split each item in binary format;
T (n) = 1250 0000.
Note ).
# Include <iostream> # include <cstdlib> using namespace STD; int f [1002]; int W [1002]; int C [52]; int T [52]; int main () {int n, m, W; while (CIN> N & N) {for (INT I = 1; I <= 50; ++ I) c [I] = 0; For (INT I = 1; I <= N; ++ I) {CIN> W; ++ C [w];} cin> m; int min = m; For (INT I = 1; I <= 50; ++ I) {If (! C [I]) continue; For (Int J = 1; j <= 50; ++ J) T [J] = C [J]; -- t [I]; // binary split int COUNT = 1; for (Int J = 1; j <= 50; ++ J) {int two = 1; while (two <t [J]) {W [count ++] = J * Two; t [J]-= two; two * = 2;} If (T [J]) W [count ++] = J * t [J];} For (Int J = 0; j <= m; ++ J) f [J] = 0; for (Int J = 1; j <count; ++ J) for (int K = m; k> = W [J]; -- K) if (F [k] <F [k-W [J] + W [J]) f [k] = f [k-W [J] + W [J]; If (M> = 5 & min> M-f [M-5]-I) min = m-f [M-5]-I;} cout <min <Endl;} return 0 ;}
HDU 2546-meal card