uva 10626 Buying Coke dfs

2 篇文章 0 订阅

题目:

F - Buying Coke

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 10626

Appoint description: 

Description

 

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 rst 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

 

3

 

2 2 1 1

 

2 1 4 1

 

20 200 3 0

 

Sample Output

 

5

 

3

题目大意:

你要去买n个可乐,每次只能买一个。且你只有a个1元,b个5元,c个10元。求你最少投多少个硬币就可以买完n个可乐

题目思路:

 

1、n最大为150,所以可以用搜索

2、重复过多,所以可以用记忆化搜索

3、每次询问都可以用上次的结果

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int inf= 1<<20;
int v[151][501][101][51];
int dfs(int n,int a,int b,int c,int ans);
int main()
{
    int ci,n,a,b,c;
    scanf("%d",&ci);
    while(ci--)
    {
        scanf("%d%d%d%d",&n,&a,&b,&c);
        printf("%d\n",dfs(n,a,b,c,0));
    }
    return 0;
}
int dfs(int n,int a,int b,int c,int ans)
{
    if(n==0)
        return 0;
    if(v[n][a][b][c]>0)
        return v[n][a][b][c];
    int da=inf;
    if(c>0)
        da=min(da,dfs(n-1,a+2,b,c-1,ans)+1);
    if(b>=2)
        da=min(da,dfs(n-1,a+2,b-2,c,ans)+2);
    if(c>0&&a>=3)
        da=min(da,dfs(n-1,a-3,b+1,c-1,ans)+4);
    if(b>0&&a>=3)
        da=min(da,dfs(n-1,a-3,b-1,c,ans)+4);
    if(a>=8)
        da=min(da,dfs(n-1,a-8,b,c,ans)+8);
    v[n][a][b][c]=da;
    //cout<<n<<a<<b<<c<<'\t'<<da<<endl;
    return da;
}

 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值