[挑战程序设计竞赛] POJ 3040 - Allowance
2014-12-19 11:56
302 查看
题意:
给定N,C分别代表面值的种类 和 每周至少要发的钱数。
接着输入N行面值 和 对应面值的个数。
问给定这些钱最多能发多少周?
思路:
1、当对应面值 >= C时,不需要贪心,直接计算即可。
2、当对应面值 < C时,按面值从大到小贪心。贪心的过程不能浪费任何面值。
3、第二步结束后,显然能不浪费的面值都已经拿完了,如果第二步贪心的结果val < C,那必然要浪费一个最小能满足val + x > C的面值。
重复上述步骤即可。。
给定N,C分别代表面值的种类 和 每周至少要发的钱数。
接着输入N行面值 和 对应面值的个数。
问给定这些钱最多能发多少周?
思路:
1、当对应面值 >= C时,不需要贪心,直接计算即可。
2、当对应面值 < C时,按面值从大到小贪心。贪心的过程不能浪费任何面值。
3、第二步结束后,显然能不浪费的面值都已经拿完了,如果第二步贪心的结果val < C,那必然要浪费一个最小能满足val + x > C的面值。
重复上述步骤即可。。
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <set> #include <map> #include <queue> #include <stack> #include <assert.h> #include <time.h> //#define _Test typedef long long LL; const int INF = 500000001; const double EPS = 1e-9; const double PI = acos(-1.0); using namespace std; int main() { #ifdef _Test freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); srand(time(NULL)); #endif int N, C, ans; pair<int, int> cost[20]; while(~scanf("%d %d", &N, &C)) { for(int i = 0; i < N; i++) { scanf("%d %d", &cost[i].first, &cost[i].second); } ans = 0; sort(cost, cost+N); for(int i = 0; i < N; i++) { if(cost[i].first >= C) { ans += cost[i].first / C * cost[i].second; cost[i].second = 0; } } while(true) { int val = C; int sum = 0; for(int i = N - 1; i >= 0; i--) { if(val && cost[i].second) { int k = min(val / cost[i].first, cost[i].second); if(k) { val -= cost[i].first * k; cost[i].second -= k; sum += cost[i].first * k; } } } for(int i = 0; i < N; i++) { if(val && cost[i].second && cost[i].first > val) { val = 0; sum += cost[i].first; cost[i].second--; break; } } if(val > 0) break; ans += sum / C; } printf("%d\n", ans); } return 0; }
相关文章推荐
- poj2456 二分搜索 <挑战程序设计竞赛>
- [挑战程序设计竞赛] POJ 3187 - Backward Digit Sums
- 挑战程序设计竞赛 poj 2431 优先队列
- poj1328 区间贪心 <挑战程序设计竞赛>
- 挑战程序设计竞赛 POJ 1192食物链 带权并查集,略麻烦
- poj2393 其它贪心 <挑战程序设计竞赛>
- [挑战程序设计竞赛] POJ 3009 - Curling 2.0
- [挑战程序设计竞赛] POJ 1862 - Stripies
- [挑战程序设计竞赛] POJ 3253 - Fence Repair
- [挑战程序设计竞赛] POJ 3050 - Hopscotch
- poj2376 区间贪心 <挑战程序设计竞赛>
- 挑战程序设计竞赛 POJ 2431 贪心+优先队列
- [挑战程序设计竞赛] POJ 3699 - Meteor Shower
- POJ 2991 Crane 线段树+计算几何 出自“挑战程序设计竞赛”
- poj1064 二分搜索 挑战程序设计竞赛
- [挑战程序设计竞赛] POJ 2718 - Smallest Difference
- poj1979解体报告(挑战程序设计竞赛)
- poj 3061 (挑战程序设计竞赛3.2.1)
- [挑战程序设计竞赛] POJ 1328 - Radar Installation
- 挑战程序设计竞赛---POJ.3233