Buying Coke +uva+记忆化搜索

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 15 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), n1n5n10 (the number of coins of value 15 and 10, respectively). The input limits are 1 <= C<= 1500 <= n1 <= 5000 <= 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值