Buying Coke +uva+记忆化搜索
来源:互联网 发布:程序员入门基础 编辑:程序博客网 时间:2024/04/28 16:27
Problem D
Buying Coke
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the values 1, 5 and 10. As soon as I press the coke button (after having inserted sufficient amount of money), I receive a coke followed by the exchange (if any). The exchange is always given in as few coins as possible (this is uniquely determined by the coin set used). This procedure is repeated until I've bought all the cokes I want. Note that I can pick up the coin exchange and use those coins when buying further cokes.
Now, what is the least number of coins I must insert, given the number of cokes I want to buy and the number of coins I have of each value? Please help me solve this problem while I create some harder problems for you. You may assume that the machine won't run out of coins and that I always have enough coins to buy all the cokes I want.
Input
The first line in the input contains the number of test cases (at most 50). Each case is then given on a line by itself. A test case consists of four integers: C (the number of cokes I want to buy), n1, n5, n10 (the number of coins of value 1, 5 and 10, respectively). The input limits are 1 <= C<= 150, 0 <= n1 <= 500, 0 <= n5 <= 100 and 0 <= n10 <= 50.
Output
For each test case, output a line containing a single integer: the minimum number of coins needed to insert into the vending machine.
Sample Input Output for Sample Input
3
2 2 1 1
2 1 4 1
20 200 3 0
5
3
148
Problem setter: Jimmy Mårdell, Member of Elite Problemsetters' Panel
解决方案:花钱方案有:8枚1硬币,3枚1硬币和1枚5硬币,2枚5硬币返2枚1硬币,1枚10硬币返2枚1硬币,1枚10硬币和3枚1硬币返1枚5硬币。其它方案都比不上这个几个优,可开个三维数组dp[i][j][k]分别是1硬币,5硬币,10硬币的剩余个数,dp表示最少投币个数,就这样用记忆化搜索显然是比较容易写的。
code:
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int dp[760][110][60];int N,C,X,Y,Z;const int maxn=0x3f3f3f3f;int d_p(int x,int y,int z){ if(dp[x][y][z]!=-1) return dp[x][y][z]; if(x+5*y+z*10==N) return dp[x][y][z]=0; int min=maxn; if(x>=8) { int k=d_p(x-8,y,z); if(k+8<min) { min=k+8; } } if(x>=3&&y>=1) { int k=d_p(x-3,y-1,z); if(k+4<min) { min=k+4; } } if(y>=2) { int k=d_p(x+2,y-2,z); if(k+2<min) { min=k+2; } } if(z>=1) { int k=d_p(x+2,y,z-1); if(k+1<min) { min=k+1; } } if(z>=1&&x>=3) { int k=d_p(x-3,y+1,z-1); if(k+4<min) { min=k+4; } } return dp[x][y][z]=min;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&C,&X,&Y,&Z); N=X+5*Y+10*Z-8*C; memset(dp,-1,sizeof(dp)); d_p(X,Y,Z); printf("%d\n",dp[X][Y][Z]); } return 0;}
- Buying Coke +uva+记忆化搜索
- uva 10626 - Buying Coke(记忆化搜索)
- UVA 10626Buying Coke(dp + 记忆化搜索)
- Uva-10626 Buying Coke DP+记忆化搜索
- uva 10626 - Buying Coke(记忆化搜索)
- uva 10626 Buying Coke (DP + 记忆化搜索)
- UVA - 10626 Buying Coke(记忆化搜索)
- uva 10626 Buying Coke (DP记忆化搜索)
- UVA 10626--Buying Coke+记忆化搜索+DP
- UVA 10626 Buying Coke (记忆化)
- uva_10626 - Buying Coke( DP,记忆化搜索 )
- uva10626 - Buying Coke 记忆化搜索
- uva10626 - Buying Coke(记忆话搜索)
- uva 10626 Buying Coke
- UVA 10626 Buying Coke
- UVA - 10626 Buying Coke
- uva 10626 - Buying Coke
- uva 10626 - Buying Coke
- C语言程序设计实践3.8 Volume
- 网络流 -- Alice's Chance(二分图多重最大匹配/最大流)
- char 与 unsigned char的本质区别
- 学习Unity3D之高级查询。
- Qt实现系统托盘
- Buying Coke +uva+记忆化搜索
- JPA初识
- 访问基类成员
- Docker 传奇之 dotCloud
- 数据结构-线性结构-堆栈
- 隐藏基类成员
- Github.com快捷键操作
- maya2014中uvlayout2.08安装
- Java学习day17