POJ 3614(最小优先队列 + 贪心算法)
来源:互联网 发布:软件开发好做吗 编辑:程序博客网 时间:2024/06/13 13:45
POJ 3614
题意:有C头牛 L 种 防晒霜,C头牛有最大和最小的承受防晒范围,L种防晒是有自己的固定值和n瓶,求多少牛可以晒太阳。
说实话:此题优先队列是为贪心而准备的。
贪心?就是从最小的防晒霜开始找,找C头牛范围小的。
想要求出最多的牛晒太阳,当然是从最小的开始遍历。
#include<iostream>#include<queue>#include<vector>#include<algorithm>using namespace std;typedef pair<int ,int > p; <span style="font-family:Courier New;">//可以看成结构体</span>p cow[2500],bot[2500];priority_queue <int ,vector<int>,greater<int> >q;int main(){ int c,l; cin>>c>>l; for(int i = 0;i < c; i++) cin>>cow[i].first>>cow[i].second; for(int i = 0;i < l; i++) cin>>bot[i].first>>bot[i].second; sort(cow,cow+c); sort(bot,bot+l); int j = 0,ans = 0; for(int i = 0;i < l; i++){ while(j < c && cow[j].first <= bot[i].first){ q.push(cow[j].second); j++; } while(!q.empty() && bot[i].second){ int x = q.top(); q.pop(); if(x < bot[i].first) continue; ans++; bot[i].second--; } } cout<<ans<<endl; return 0;}
用结构体写:
#include<iostream>#include<queue>#include<vector>#include<algorithm>using namespace std;class p{public: int first; int second;};bool cmp(p a,p b){ return a.first<b.first;}p cow[2500],bot[2500];priority_queue <int ,vector<int>,greater<int> >q;int main(){ int c,l; cin>>c>>l; for(int i = 0;i < c; i++) cin>>cow[i].first>>cow[i].second; for(int i = 0;i < l; i++) cin>>bot[i].first>>bot[i].second; sort(cow,cow+c,cmp); sort(bot,bot+l,cmp); int j = 0,ans = 0; for(int i = 0;i < l; i++){ while(j < c && cow[j].first <= bot[i].first){ q.push(cow[j].second); j++; } while(!q.empty() && bot[i].second){ int x = q.top(); q.pop(); if(x < bot[i].first) continue; ans++; bot[i].second--; } } cout<<ans<<endl; return 0;}
0 0
- POJ 3614(最小优先队列 + 贪心算法)
- POJ 3614 Sunscreen (贪心、优先队列)
- POJ 3614 Sunscreen(贪心+优先队列)
- poj 3614Sunscreen(优先队列和贪心)
- POJ 3614 Sunscreen 优先队列 贪心
- POJ 3614 Sunscreen 贪心 优先队列
- Sunscreen (poj 3614 贪心+优先队列)
- POJ 3614:Sunscreen 贪心+优先队列
- poj 3614 Sunscreen 贪心 优先队列
- poj 3614 Sunscreen 贪心+优先队列
- POJ 3614 Sunscreen(贪心+优先队列)
- Poj 3614 Sunscreen【贪心+优先队列】
- POJ-3614--贪心+优先队列---Sunscreen
- Sunscreen (poj 3614 贪心+优先队列)zai
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- POJ-3190-贪心(优先队列)
- poj 2431 Expedition (贪心+优先队列)
- eclipse+maven+tomcat7+hot deploy
- PHP + JavaScript + Ajax 实现无刷新页面加载效果
- 【cocos2d-x从c++到js】JS与C++的交互3——C++和JS类型转换
- hdu 5835 Danganronpa
- 黄阶低级 - 《 Java 核心》- 3.9 大数值 - 修改0次
- POJ 3614(最小优先队列 + 贪心算法)
- 第六条:消除过期的引用变量
- 排序算法(JAVA实现)
- java 学习第一节--基本语法
- VS2015编译Qt5.7静态库
- php中一个header()跳转到另外一个页面后要加exit()
- 07、canvas标签之canvas变换
- 【数据库】数据库的四个范式之间的区别
- 多postgresql库批量执行SQL脚本文件