小白dp 10626 - Buying Coke

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

 


题意:

用几个1、5、10的硬币去买几瓶价值8元的饮料,问最少投多少个硬币能买到。(一次只能买一瓶饮料,机器按照找你最少的硬币找钱)


思路:

如果四维dp的话,时间、空间肯定都吃不消,然后仔细分析,3维就够了,3维确定了,第四维也就定了。

dp[x][y][z]表示还剩x个1元、y个5元、z个10元时还需投入多少个硬币满足条件。(此时还剩几瓶饮料要买是确定的)

因为1、5的硬币能够变多,故要估计下一数组开多大。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 100005
#define mod 1000000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,cnt,tot,flag;
int a,b,c;
int dp[705][150][51];

int dfs(int num,int x,int y,int z)
{
    if(num==0) return 0;
    if(dp[x][y][z]!=INF) return dp[x][y][z];
    int i,j,t,best=INF;
    if(x>=8)
    {
        t=dfs(num-1,x-8,y,z)+8;
        best=min(best,t);
    }
    if(y>=2)
    {
        t=dfs(num-1,x+2,y-2,z)+2;
        best=min(best,t);
    }
    if(z>=1)
    {
        t=dfs(num-1,x+2,y,z-1)+1;
        best=min(best,t);
    }
    if(x>=3&&y>=1)
    {
        t=dfs(num-1,x-3,y-1,z)+4;
        best=min(best,t);
    }
    if(x>=3&&z>=1)
    {
        t=dfs(num-1,x-3,y+1,z-1)+4;
        best=min(best,t);
    }
    dp[x][y][z]=best;
    return best;
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&a,&b,&c);
        memset(dp,0x3f,sizeof(dp));
        ans=dfs(n,a,b,c);
        printf("%d\n",ans);
    }
    return 0;
}
/*
3
2 2 1 1
2 1 4 1
20 200 3 0
*/






你可能感兴趣的:(小白dp 10626 - Buying Coke)