HDU 5527 Too Rich (好题 贪心 DFS)
来源:互联网 发布:python开发小游戏 编辑:程序博客网 时间:2024/05/04 00:07
Too Rich
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 228 Accepted Submission(s): 69
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, ifp=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.
For example, if
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
317 8 4 2 0 0 0 0 0 0 0100 99 0 0 0 0 0 0 0 0 02015 9 8 7 6 5 4 3 2 1 0
Sample Output
9-136
Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5527
题目大意:有1,5,10,20,50,100,200,500,1000,2000十种面值的钱币各ci个,现在要凑出p元,问最多可以用多少钱币凑出
题目分析:长春的金牌题。。。首先按贪心的思想,用尽可能多的小面值钱币,前提是小面值钱币可以凑出当前需要的钱数,所以从大面值的开始决策,比如现在到第idx个面值的钱币,要凑x元,又用1~idx-1的钱币可以凑出的总金额为y元,那么当前面值我需要用(x - y) / c[i]个,当然如果这个值小于0,就不用当前面值的钱币,注意如果c[i]不能整除(x- y),则需要多用一个idx的钱币,因为剩下的钱不够,比如有 10 20 20 50 50,现在要凑110,110 - 10 - 20 - 20 = 60,50不能整除60,则就需要两个50的,因为只用一个50的话,剩下的凑不出60,还有一点要注意的是20不能整除50,200不能整除500,因此我们算个数的时候有时需要多加一个,比如20 20 20 50,现在要凑50,因为剩下3个20,一共可以凑60,按照贪心策略那个单独的50就不会被选了,因此这里需要强制选一个50,200和500同理
#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int p, ans, c[11];int val[11] = {0, 1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000}; ll sum[11];void DFS(int rest, int idx, int cnt){ if(rest < 0) return; if(idx == 0) { if(rest == 0) ans = max(ans, cnt); return; } ll cur = max(rest - sum[idx - 1], 0ll); int curnum = cur / val[idx]; if(cur % val[idx]) curnum ++; if(curnum <= c[idx]) DFS(rest - curnum * val[idx], idx - 1, cnt + curnum); curnum ++; if(curnum <= c[idx]) DFS(rest - curnum * val[idx], idx - 1, cnt + curnum);}int main(){ int T; scanf("%d", &T); while(T --) { memset(sum, 0, sizeof(sum)); ans = -1; scanf("%d", &p); for(int i = 1; i <= 10; i++) scanf("%d", &c[i]); for(int i = 1; i <= 10; i++) sum[i] = sum[i - 1] + (ll)(val[i] * c[i]); DFS(p, 10, 0); printf("%d\n", ans); }}
0 0
- HDU 5527 Too Rich (好题 贪心 DFS)
- HDU 5527 Too Rich(dfs贪心)
- HDU 5527 Too Rich (贪心+DFS)
- HDU 5527 Too Rich 【DFS】
- HDU 5527(Too Rich-贪心)
- hdu 5527 Too Rich(贪心)
- HDU5527:Too Rich(DFS & 贪心 & 思维)
- hdu 5527 Too Rich
- HDU 5527----Too Rich
- HDU 5527 Too Rich (2015长春站A题&&贪心)
- 【hdu 5527】 [2015ACM/ICPC亚洲区长春站] Too Rich 贪心
- Hdu 5242 Game【Dfs+贪心】好题~
- Too Rich HDU
- hdu5527 Too Rich(贪心)
- HDU 5927 Auxiliary Set DFS好题
- hdu 5073 Galaxy【思维+贪心】好题
- hdu5527 Too Rich
- hdu5527 Too Rich
- _MSC_VER宏
- iOS消息传递机制---KVC和KVO
- android中像素单位dp、px、pt、sp的比较
- 关于在ie8下背景图不识别background-size的问题
- linux ls -l 详解
- HDU 5527 Too Rich (好题 贪心 DFS)
- 用java实现反转链表
- 菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体
- ORACLE数据库:日期时间函数大全
- Android 多点触控技术实战,自由地对图片进行缩放和移动
- OpenJudge1665 完美覆盖
- 常见的数字证书格式与协议简介
- 单例的书写
- 再谈IE的浏览器模式和文档模式