*Allowance(POJ 3040, 贪心)
来源:互联网 发布:白银交易软件 编辑:程序博客网 时间:2024/06/06 09:43
题目链接
题目大意:FJ每周给Bessie最少C,FJ有各种面值的钱各多少,求最多能支付多少周。
解题思路:从大到小凑近或等于C但不能等于,然后从小到大凑够C(等于或大于C)。
/*大于C的coin每周一个发给Bessie就好了 小于C的: 从cent最大的开始取,凑近C但不能等于C 从cent最小的开始取,等于或大于C */#include<cstdio>#include<algorithm> using namespace std;const int MAX_N = 25;int N, C, ans;struct coin {int cent;int num;} a[MAX_N];bool cmp(coin b, coin c){return b.cent > c.cent;}void init(){ans = 0;//需要吗? for(int i=0; i<N; i++){scanf("%d%d", &a[i].cent, &a[i].num);} }void f(){int i, j = N-1, sum = 0;for(i=0; i<N && a[i].cent>=C; i++){ans += a[i].num;a[i].num = 0; } out:for(i=0; i<N; i++){while(a[i].num>0){//从cent最大的开始取凑近或等于C但不大于C sum += a[i].cent;a[i].num--;if(sum == C){ans++; sum = 0;goto out;}else if(sum > C){sum -= a[i].cent;a[i].num++;break;}} }//从cent最小的开始取直到凑够C for(; j>=0; j--){ while(a[j].num > 0){ sum += a[j].cent; a[j].num--; if(sum >= C){ ans++; sum = 0;goto out;}} } }int main(){while(~scanf("%d%d", &N, &C)){init();sort(a, a+N, cmp);f();printf("%d\n", ans);//记得删\n!!! }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 贪心
- 有关字符常量存储引起的数据在内存中存储的若干问题
- React-Quill中的图片上传及显示
- SVM
- c# 抓取数据的3种方法
- 数据结构实验之查找七:线性之哈希表
- *Allowance(POJ 3040, 贪心)
- 1215-Cannot add the foreign key constraint
- 多张图片上传
- 网络加载购物车布局
- Java设计模式之备忘录模式
- C#中的virtual,abstract,override和new关键字小结
- goim(2):编译构建 goim 项目
- 域名访问和ip访问引起的http 403问题
- 仿京东购物车专用适配器