uva 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 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), n1n5n10 (the number of coins of value 15 and10, 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

直接开四维dp搜索,存不下。但是!c,n1,n5,n10是有一个确定关系的。那么就可以少记录一维了,消掉范围最大的n1.就存的下了。这个题可以学到的是,如果状态太大存不下,可以想想某一维是不是可以由其它几维直接确立。

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stack>
#include<iostream>
#include<queue>
#include<cmath>
#include<string>
#include<set>
#include<map>
using namespace std;
const int maxn = 10000000 + 5;
const int INF = 1000000000;
const int Mod = 1000000000 + 7;
typedef long long LL;
typedef pair<LL, LL> P;

int dp[155][155][55];
int C, total;

int dfs(int c, int n5, int n10){
    if(dp[c][n5][n10] != -1)
        return dp[c][n5][n10];
    int n1 = total-(C-c)*8 - n5*5-n10*10;
    if(c == 0)
        return dp[0][n5][n10] = 0;
    int Min = INF;
    if(n1 >= 8){
        Min = min(Min, 8+dfs(c-1, n5, n10));
    }
    if(n1 >= 3 && n5 >= 1){
        Min = min(Min, 4+dfs(c-1, n5-1, n10));
    }
    if(n1 >= 3 && n10 >= 1){
        Min = min(Min, 4+dfs(c-1, n5+1, n10-1));
    }
    if(n5 >= 2){
        Min = min(Min, 2+dfs(c-1, n5-2, n10));
    }
    if(n10 >= 1){
        Min = min(Min, 1+dfs(c-1, n5, n10-1));
    }
    return dp[c][n5][n10] = Min;
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        int n1, n5, n10;
        scanf("%d%d%d%d", &C, &n1, &n5, &n10);
        total = n1 + 5*n5 + 10 * n10;
        memset(dp, -1, sizeof dp);
        int ans = dfs(C, n5, n10);
        printf("%d\n", ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值