Topic Link: Click to enter
The original definition state Dp[n][n1][n5][n10] means the number of coins to be spent on the remaining 1,5,10 coins n1,n5,n10 when buying n bottles of Coke. Then the state transfer is: 1.8 One cent coins purchase of the nth bottle Cola, t=dp[n-1][n1+8][n5][n10]+8; 2. A five-point and 3-1-point, t=dp[n-1][n1+3][n5+1][n10]+4; 3. Two of 5 t=dp[n-1][n1][n5+2][n10]; 4. A 10-point, t=dp[n-1][n5][n10+1].
For the 12th type of dp[n][n1][n5][n10]=min (T,dp[n][n1][n5][n10])
For 3, 4 kinds: dp[n][n1+2][n5][n10]=min (dp[n][n1+2][n10],t).
Then hand over the time-out, after looking at the other people's ideas, send themselves less consider a situation: Cast 3 1 points and a 10, get back a 5 points. Then you can write it with a memory search. Define status Dp[n1][n5][n10] indicates the minimum number of coins required to buy a bottle of Coke under the status (N1,N5,N10).
The code is as follows:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define INF 0x3f3f3f3fintdp[780][ the][ -];intSolveintNintN1,intN5,intN10) {if(dp[n1][n5][n10]!=-1)returnDP[N1][N5][N10];if(n==0)return 0;intMin=inf;if(n1>=8) {intT=solve (n1, n1-8, N5,N10) +8; Min=min (min,t); }if(n1>=3&&n5>=1) {intT=solve (n1, n1-3, n5-1, N10) +4; Min=min (min,t); }/ * if (n1>=3&&n10>=1) {int t=solve (n-1,n1-3,n5+1,n10-1) +4; Min=min (t,min); }*/ if(n5>=2) {intT=solve (n1, n1+2, n5-2, N10) +2; Min=min (t,min); }if(n10>=1) {intT=solve (n1, n1+2, n5,n10-1)+1; Min=min (min,t); }returnDp[n1][n5][n10]=min;}intMain () {//Freopen ("In.txt", "R", stdin); intT,N,N1,N5,N10;scanf("%d", &t); while(t--) {scanf("%d%d%d%d", &N,&N1,&N5,&N10);memset(dp,-1,sizeof(DP));intAns=solve (N,N1,N5,N10);printf("%d\n", ans); }return 0;}
UVA 10626--buying coke+ Memory Search +DP