Ultraviolet A 10626 Buying Coke (memory-based)
Address: Click to open the link
Buy an 8-point drink, and you have three coins: 1, 5, and 10.
Then I asked you to buy a c-bottle drink, and buy one at a time. You have the minimum number of coins.
There are several ways to do this: 1: cast 8 points and 2: cast a 5-point 3 points and 1 point
3: If you place a score of 10, find the score of 3. If you place a score of 10, find the score of 5.
There are other solutions, but they are not too cost-effective.
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
pil;const int INF = 0x3f3f3f3f;const int maxn=1100;char str[maxn];int dp[1100][155][55];int n1,n2,n3,c;int dfs(int num,int x1,int x2,int x3)//num,1,5,10{ if(num==0) return 0; int &res=dp[x1][x2][x3]; if(res!=-1) return res; res=INF; if(x3>=1) res=min(res,dfs(num-1,x1+2,x2,x3-1)+1); if(x2>=2) res=min(res,dfs(num-1,x1+2,x2-2,x3)+2); if(x1>=8) res=min(res,dfs(num-1,x1-8,x2,x3)+8); if(x1>=3&&x3>=1) res=min(res,dfs(num-1,x1-3,x2+1,x3-1)+4); if(x1>=3&&x2>=1) res=min(res,dfs(num-1,x1-3,x2-1,x3)+4); return res;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&c,&n1,&n2,&n3); CLEAR(dp,-1); printf("%d\n",dfs(c,n1,n2,n3)); } return 0;}