16BBurglar and Matches
来源:互联网 发布:软件比赛有哪些 编辑:程序博客网 时间:2024/05/19 20:23
题意:给你n个你需要带走的箱子,m个大集装箱,每个集装箱里有a个盒子,每个盒子里面有b根火柴。输出能拿走n个盒子的火柴最大数。
题解:贪心,用结构体排序每个盒子里面最大的火柴数,每次拿最多火柴的火柴盒,用n减去拿掉的,直到n为0.最开始还想前缀和,很麻烦。看了一下博客都是贪心,就贪心咯。
#include<bits/stdc++.h>using namespace std;struct node{ int a; int b;} q[25];bool cmp(node a,node b){ return a.b>b.b;}int main(){ int n,m; long long ans; while(cin>>n>>m) { ans=0; for(int i=0; i<m; i++) cin>>q[i].a>>q[i].b; sort(q,q+m,cmp); for(int i=0; i<m; i++) { if(n>=q[i].a) { ans+=q[i].a*q[i].b; n-=q[i].a; } else { ans+=n*q[i].b; break; } } cout<<ans<<endl; } return 0;}
阅读全文
0 0
- 16BBurglar and Matches
- 16B. Burglar and Matches
- CodeForces 16B Burglar and Matches(贪心)
- CodeForces 16B Burglar and Matches (贪心)
- CodeForces 16B Burglar and Matches
- matches()
- fatal: --author '' is not 'Name <email>' and matches no existing author
- fatal: --author 'user@mail.com' is not 'Name <email>' and matches no existing author
- --author 'yidasanqian' is not 'Name <email>' and matches no existing author
- Matches Game
- java matches
- matches()总结
- Matches UVA
- String的matches使用方法
- 13.2 匹配集合Matches
- Matches 集合解说
- [poj2234]Matches Game
- 2234-Matches Game-博弈
- Java开发环境配置
- InteilJ Idea 的java文件夹文本文件无法复制到输出目录
- 第4周 【项目三】 单链表应用
- 交叉熵crossEntropyLoss
- Javascript基础1
- 16BBurglar and Matches
- 测试bbr性能
- U盘插3.0口不识别,插2.0口识别
- 原码反码与补码
- ruby中attr_accessor方法的理解
- Myeclipse8.5 添加Tomcat7
- Python 深入浅出
- STL常用容器内部实现
- Struts2学习笔记——Struts2概览