HDU 5527 Too Rich 【DFS】


Too Rich

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit:262144/262144 K (Java/Others)
Total Submission(s): 1342    Accepted Submission(s): 335

Problem Description

You are a rich person, and you think your wallet is too heavy and full now. So youwant to give me some money by buying a lovely pusheen sticker which costs p dollars fromme. To make your wallet lighter, you decide to pay exactly p dollars by asmany coins and/or banknotes as possible.

For example, if
p=17 and you havetwo $10 coins,four $5 coins,and eight $1 coins,you will pay it by two $5 coinsand seven $1 coins.But this task is incredibly hard since you are too rich and the sticker is tooexpensive and pusheen is too lovely, please write a program to calculate thebest solution.

 

 

Input

The first line contains an integer T indicating the total number of testcases. Each test case is a line with 11 integers p,c1,c5,c10,c20,c50,c100,c200,c500,c1000,c2000, specifying theprice of the pusheen sticker, and the number of coins and banknotes in eachdenomination. The number ci means how manycoins/banknotes in denominations of i dollars in yourwallet.

1≤
T≤20000
0≤
p≤109
0≤ci≤100000

 

 

Output

For each test case, please output the maximum number of coins and/or banknotes hecan pay for exactly p dollarsin a line. If you cannot pay for exactly p dollars, pleasesimply output '-1'.

 

 

Sample Input

3

17 8 4 2 0 0 0 00 0 0

100 99 0 0 0 0 00 0 0 0

2015 9 8 7 6 5 43 2 1 0

 

 

Sample Output

9

-1

36



【题意】


现在有面值为1,5,10,20,50,100,200,500,1000,2000的十种硬币若干,问能否凑出p元钱,若能凑出,所使用的硬币最多有多少个。


【思路】


先把问题转化为我们熟悉的:假设所有硬币的价值总和为sum,我们需要求的便是能否用最少的硬币凑出(sum-p)元钱。


我们知道如果每种硬币的数量没有限制且面值大的价值正好是面值小的价值的整数倍,那么我们只要贪心地从大到小去凑即可。


但是由于这里出现了50,500这两个不满足要求的面值,比如现在有20,20,20,50,50四枚硬币要凑出110元,显然如果用刚才的方法就凑不出来了,我们可以考虑少拿一个50的硬币,这样就能按刚才的方法凑出来了。


所以我们只要搜索一遍即可,同时注意多搜索少拿的情况。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 1005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int ans;
int val[10]={1,5,10,20,50,100,200,500,1000,2000};
int num[10];

void dfs(int pos,int sum,int total)
{
    if(sum==0)
    {
        ans=min(ans,total);
        return;
    }
    if(pos==-1) return;
    int temp=min(num[pos],sum/val[pos]);
    dfs(pos-1,sum-temp*val[pos],total+temp);
    if(temp>0)
    {
        temp--;
        dfs(pos-1,sum-temp*val[pos],total+temp);
    }
}

int main()
{
    int p;
    rush()
    {
        int sum=0;
        int total=0;
        scanf("%d",&p);
        for(int i=0;i<10;i++)
        {
            scanf("%d",&num[i]);
            total+=num[i];
            sum+=num[i]*val[i];
        }
        sum-=p;
        if(sum<0)
        {
            puts("-1");
            continue;
        }
        ans=INF;
        dfs(9,sum,0);
        if(ans==INF) puts("-1");
        else printf("%d\n",total-ans);
    }
    return 0;
}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值