POJ 3614(最小优先队列 + 贪心算法)
2016-08-15 17:34
190 查看
POJ 3614
题意:有C头牛 L 种 防晒霜,C头牛有最大和最小的承受防晒范围,L种防晒是有自己的固定值和n瓶,求多少牛可以晒太阳。
说实话:此题优先队列是为贪心而准备的。
贪心?就是从最小的防晒霜开始找,找C头牛范围小的。
想要求出最多的牛晒太阳,当然是从最小的开始遍历。
用结构体写:
题意:有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; }
相关文章推荐
- POJ - 3659 Cell Phone Network 最小支配集(树的) 贪心算法
- poj 3026 Borg Maze bfs建图+最小生成树
- poj 3204 最小割(关键割边)
- POJ 2031 Building a Space Station (最小生成树)
- POJ 1328 Radar Installation 贪心算法
- POJ 2485 : 最小生成树(kruskal+并查集)
- poj 2125(最小割)
- [有向树的最小表示] poj 1635 Subway tree systems
- POJ 2395 Out of Hay(最小瓶颈生成树)
- poj 2031 Building a Space Station 最小生成树
- POJ 2125 Destroying The Graph(二分图最小点权覆盖)
- poj 1258 最小生成树
- 周赛 POJ 2060 最小路径覆盖
- POJ 1287 Networking(最小生成树)
- poj 3264 Command Network(最小树形图)
- POJ 3659 树图的最小支配集
- POJ 1287 Networking(最小生成树)
- POJ 1966 无向图点联通度 最小割
- POJ 一 2485 Highways(最小生成树)
- POJ 1258:Agri-Net Prim最小生成树模板题