POJ 3040 - Allowance(贪心)
来源:互联网 发布:海牛大数据 编辑:程序博客网 时间:2024/05/20 08:22
Description
As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute the maximum number of weeks he can pay Bessie.
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.
* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.
Output
* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance
Sample Input
3 610 11 1005 120
Sample Output
111------------------------------------------------
思路:
贪心三个步骤:
1:面额大于等于c的硬币,无法节约,全部先发掉。
2:然后对硬币面额从大到小尽量凑得接近C,允许等于或不足C,但是不能超出C
3:接着按硬币面额从小到大凑满C(凑满的意思是允许超出一个最小面值,ps此处的最小面值指的是硬币剩余量不为0的那些硬币中的最小面值),凑满之后得出了最优解,发掉。再进入步骤2,直到剩下的钱数无法凑满c。
CODE:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct node { int val, num; bool operator < (const node &a) const{ return val > a.val; }}coin[25];int need[25];int main(){//freopen("in", "r", stdin); int n, c; while(~scanf("%d %d", &n, &c)) { int ans = 0, nj = 0; for(int i = 0; i < n; ++i) { int v, b; scanf("%d %d", &v, &b); if(v >= c) ans += b; else { coin[nj].val = v; coin[nj++].num = b; } } sort(coin, coin + nj); while(1) { int res = c; memset(need, 0, sizeof(need)); for(int i = 0; i < nj; ++i) { if(res && coin[i].num) { int c = res / coin[i].val; c = min(c, coin[i].num); need[i] = c; res -= c*coin[i].val; } } if(res) { for(int i = nj-1; i >= 0; --i) { if(coin[i].val >= res && (coin[i].num - need[i])) { need[i]++; res = 0; break; } } if(res) break; } int d = 10000000; for(int i = 0; i < nj; ++i) { if(need[i]) { d = min(d, coin[i].num/need[i]); } } ans += d; for(int i = 0; i < nj; ++i) { coin[i].num -= d*need[i]; } } printf("%d\n", ans); } return 0;}
0 0
- poj 3040 Allowance (贪心)
- POJ 3040 - Allowance(贪心)
- POJ 3040 Allowance (贪心)
- POJ 3040- Allowance(贪心)
- [POJ 3040] Allowance (贪心)
- 【POJ】3040 - Allowance(贪心)
- *Allowance(POJ 3040, 贪心)
- POJ 3040 Allowance 贪心
- poj 3040 Allowance 贪心
- 贪心-poj-3040-Allowance
- poj 3040 Allowance(贪心)
- poj 3040 Allowance (贪心)
- Allowance - POJ 3040 贪心
- Allowance (poj 3040 贪心)
- poj 3040 Allowance贪心
- POJ 3040 Allowance 贪心
- POJ 3040 Allowance 贪心
- POJ 3040 Allowance 贪心
- debian wheezy 7.8无线网卡的安装
- ZOJ 1047 Image Perimeters
- android开发学习--------Activity的创建及数据传递f
- 【Jason's_ACM_解题报告】Quadtrees
- 双栈结构
- POJ 3040 - Allowance(贪心)
- 蓝桥杯之带分数
- zoj 1298 && poj 1135 Domino Effect
- 搭建hbase
- hdu 2546 饭卡
- Struts2通过请求参数值指定配置的结果
- web开发N例-案例2:PHP绘制图片、绘制中文
- 修改discuz模板都需要注意什么
- HDU 5150 && UVALive 5061 (LCA标记)