HDU 5527 Too Rich

Too Rich

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

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

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

Input
The first line contains an integer T indicating the total number of test cases. Each test case is a line with 11 integers p,c1,c5,c10,c20,c50,c100,c200,c500,c1000,c2000, specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. The number ci means how many coins/banknotes in denominations of i dollars in your wallet.

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

Output
For each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. If you cannot pay for exactly p dollars, please simply output '-1'.

Sample Input
3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0
 

 

Sample Output
9
-1
36
 

 

Source
 
解题:
看看 hqwhqwhq的解法
贪心,用尽量多的小面值的纸币来凑数,基本上每一个数都能整除后面一个数,所以前 i种面值最多能凑出$sum[i]$元,那么第$i+1$种面值需要$\frac{p-sum[i]}{cnt[i+1]}$
此时有个小问题就是,20并不能整除50,但是20|2∗50,所以当面值为50,500的时候,可能需要多取一张。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 using LL = long long;
 4 const int maxn = 11;
 5 const int val[] = {0, 1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000};
 6 int cnt[maxn],ret;
 7 LL sum[maxn];
 8 void dfs(LL rest,int pos,int cnt){
 9     if(rest < 0) return;
10     if(!pos){
11         if(!rest) ret = max(ret,cnt);
12         return;
13     }
14     LL tmp = max(0LL,rest - sum[pos-1]);
15     int tnt = tmp/val[pos];
16     if(tmp%val[pos]) ++tnt;
17     if(tnt <= ::cnt[pos]) dfs(rest - (LL)tnt*val[pos],pos - 1,cnt + tnt);
18     if(++tnt <= ::cnt[pos]) dfs(rest - (LL)tnt*val[pos],pos - 1,cnt + tnt);
19 }
20 int main(){
21     int kase,money;
22     scanf("%d",&kase);
23     while(kase--){
24         scanf("%d",&money);
25         for(int i = 1; i < maxn; ++i){
26             scanf("%d",cnt + i);
27             sum[i] = sum[i - 1] + static_cast<LL>(cnt[i])*val[i];
28         }
29         ret = -1;
30         dfs(money,10,0);
31         printf("%d\n",ret);
32     }
33     return 0;
34 }
35 
36  
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4956390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值