UVa 11264 Coin Collector (选硬币&贪心好题)
来源:互联网 发布:易知投资有限责任公司 编辑:程序博客网 时间:2024/06/05 05:58
11264 - Coin Collector
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2231
Our dear Sultan is visiting a country where there are n different types of coin. He wants to collect as many different types of coin as you can. Now if he wants to withdraw X amount of money from a Bank, the Bank will give him this money using following algorithm.
withdraw(X){
if( X == 0) return;
Let Y be the highest valued coin that does not exceed X.
Give the customer Y valued coin.
withdraw(X-Y);
}
Now Sultan can withdraw any amount of money from the Bank. He should maximize the number of different coins that he can collect in a single withdrawal.
Input:
First line of the input contains T the number of test cases. Each of the test cases starts with n (1≤n≤1000), the number of different types of coin. Next line contains n integers C1, C2, ... , Cn the value of each coin type. C1<C2<C3< … <Cn<1000000000. C1 equals to 1.
Output:
For each test case output one line denoting the maximum number of coins that Sultan can collect in a single withdrawal. He can withdraw infinite amount of money from the Bank.
Sample Input
Sample Output
2
6
1 2 4 8 16 32
6
1 3 6 8 15 20
6
4
思路:
1. 你肯定注意到了,面值最大的硬币c[n-1]必须要选。(反证:如果花了sum元却没有选中它,可知sum<c[n-1],于是用m+c[n-1]元去兑换可以得到一个更优解。)
2. 贪心的关键:假设S(i)是c[1]…c[i] 中那些被选中的货币的面值的和,那么一定有 S(i) < c[i+1]。(反证:若S(i)>=c[i+1],那银行肯定要找给他c[i+1]面值的硬币。)
3. 所以可以构造一个这样的序列出来。按照2中所说,如果有 S(i-1) < c[i]且S(i) =S(i-1)+c[i]<c[i+1],那么c[i]将被选中。
完整代码:
/*0.015s*/#include<cstdio>int c[1005];int main(){int t, n, i, sum, count;scanf("%d", &t);while (t--){scanf("%d", &n);for (i = 0; i < n; ++i)scanf("%d", &c[i]);if (n <= 2) printf("%d\n", n);else{sum = c[0], count = 2;///先把第一个和最后一个算上for (i = 1; i < n - 1; ++i)if (sum < c[i] && sum + c[i] < c[i + 1])sum += c[i], ++count;printf("%d\n", count);}}return 0;}
- UVa 11264 Coin Collector (选硬币&贪心好题)
- uva 11264 - Coin Collector(dp or 贪心)
- UVa11264 - Coin Collector(贪心)
- EOJ1488 COIN COLLECTOR 贪心
- UVA 674 Coin Change 换硬币 经典dp入门题
- HDU2069 & UVA 674 Coin Change(换硬币 dp 入门经典水题,背包问题)
- coin collector(一道测试题)
- Coin Change - UVa 674 换硬币的dp
- UVa 1346 Songs (贪心好题)
- 硬币 coin题解
- Coin Change【硬币找零】
- uva 12260 - Free Goodies (dp,贪心 | 好题)
- UVA 1599 Ideal Path 【两次BFS+贪心】 (好题)
- uva674 - Coin Change(硬币找零)
- Coin Change 2-硬币问题
- EOJ 1488 Solution Report - Coin Collector
- hdu4415 贪心好题
- 2014-2015 CT S02E10 C题 Coin Graph 构造+贪心
- 如何驱动一款USB设备
- BitMap的java实现
- spoj AMR11 Robbing Gringotts 双边暴力+hash+费用流
- EPP编写代码浏览器无法打开问题
- 线程同步之临界区(类比互斥对象进行分析)
- UVa 11264 Coin Collector (选硬币&贪心好题)
- HDOJ, 杭电2050折线分割平面
- JavaSwing图形界面编程之自定义类(一)
- 解决ok6410 uboot下ping不通的问题
- c\c++复习基础要点10---智能指针
- ADO.NET
- 调查问卷录入spss
- js异步加载
- 关于分割平面问题的总结