Question: uva10626-buying Coke)
I want to give you three valuable coins: 1, 5, 10. Now I want you to buy a vending machine to buy cola, a bottle of cola is worth 8, and I want you to buy the number of cola, and the number of three coins, ask how many coins you need at least. The automatic sales opportunity is changed to zero Based on the money you have invested. If you can, you will find at least the change coins.
Solution: I did not think that the number of bottles you have purchased for cola is hidden in the remaining coin situation. In other words, it is how many bottles of cola you have bought, no matter how you can buy it, the remaining coins always reflect how many bottles of cola are bought. Institute
You can open a three-dimensional array. In addition, pay attention to the situation where one 10 and three 1 buy one Coke find one 5. Transfer status (2 5 find 2 1) (1 10 find 2 1) (1 5 3 1 find 0) (1, 10, 3, 1, and 5) the last 8 can be put into consideration at the end, because this is the worst case, so when the 5 and 10 coins are gone, use 1 to buy them.
Code:
# Include <cstdio> # include <cstring> const int n1 = 705; const int n2 = 155; const int N3 = 55; const int INF = 0x3f3f3f; int f [N1] [n2] [N3]; void Init () {for (INT I = 0; I <N1; I ++) for (int K = 0; k <N2; k ++) for (int l = 0; L <N3; l ++) f [I] [k] [l] = inf ;} int min (const int A, const int B) {return a <B? A: B;} int dp (INT C, int N1, int N2, int N3) {// printf ("% d \ n", C, n1, N2, N3); Int & Ans = f [N1] [n2] [N3]; If (ANS! = Inf) return ans; If (C = 0) return ans = 0; If (! N2 &&! N3) return ans = 8 * C; If (N3) ans = min (ANS, dp (c-1, N1 + 2, N2, N3-1) + 1 ); if (N2> = 2) ans = min (ANS, dp (c-1, N1 + 2, n2-2, N3) + 2 ); if (N2 & N1> = 3) ans = min (ANS, dp (c-1, N1-3, n2-1, N3) + 4 ); if (N3 & N1> = 3) ans = min (ANS, dp (c-1, N1-3, n2 + 1, N3-1) + 4 ); return ans;} int main () {int t; int C, N1, N2, N3; scanf ("% d", & T); While (t --) {scanf ("% d", & C, & N1, & N2, & N3); Init (); printf ("% d \ n ", DP (C, N1, N2, N3);} return 0 ;}
Uva10626-buying Coke)