POJ3614Sunscreen(优先队列+贪心)
Sunscreen
| Time Limit:?1000MS | ? | Memory Limit:?65536K |
| Total Submissions:?8435 | ? | Accepted:?2981 |
Description
To avoid unsightly burns while tanning, each of the?C?(1 ≤?C?≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow?i?has a minimum and maximum?SPF?rating (1 ≤?minSPFi?≤ 1,000;?minSPFi?≤?maxSPFi?≤ 1,000) that will work. If the?SPF?rating is too low, the cow suffers sunburn; if the?SPF?rating is too high, the cow doesn't tan at all........
The cows have a picnic basket with?L?(1 ≤?L?≤ 2500) bottles of sunscreen lotion, each bottle?i?with an?SPF?rating?SPFi?(1 ≤?SPFi?≤ 1,000). Lotion bottle?i?can cover?coveri?cows with lotion. A cow may lotion from only one bottle.
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
Input
* Line 1: Two space-separated integers:?C?and?L
* Lines 2..C+1: Line?i?describes cow?i's lotion requires with two integers:?minSPFi?and?maxSPFi?
* Lines?C+2..C+L+1: Line?i+C+1 describes a sunscreen lotion bottle?i?with space-separated integers:?SPFi?and?coveri
Output
A single line with an integer that is the maximum number of cows that can be protected while tanning
Sample Input
3 2 3 10 2 5 1 5 6 2 4 1Sample Output
2Source
USACO 2007 November Gold
題意:有C頭奶牛要去沐光浴,太陽光太強烈會曬壞皮膚,太弱又會沒效果。每頭牛都有一個太陽光適宜的范圍經行沐光浴,分別給出minspf_i和maxspf_i。 有L種防曬霜,每種防曬霜可以把所受陽光固定于一個值spf_i,每種有cover_i瓶。 問最多會有幾頭牛得到合適的光曬強度?
題解:
貪心策略,在滿足minspf的條件下,盡量將spf的防曬霜涂到maxspf小的奶牛身上,因為maxspf大的奶牛有更多的選擇。
這里就需要一個優先隊列來儲存滿足minspf的奶牛的maxspf的值。 具體解題步驟如下:
1.將奶牛按照minspf升序排列,將防曬霜按照spf升序排列。
2.枚舉防曬霜,將minspf<=spf的奶牛的maxspf存到優先隊列中,然后值小的先出隊列,
看是否滿足maxspf>=spf,更新記錄值。
#include<bits/stdc++.h> using namespace std; struct cow {int min_spf,max_spf; }; struct suns {int spf,num; }; vector<cow> c; vector<suns> s; int n,m; bool cmp1(cow a,cow b) {return a.min_spf<b.min_spf; } bool cmp2(suns a,suns b) {return a.spf<b.spf; } priority_queue<int,vector<int>,greater<int> >qua; int main() {cin>>n>>m;for(int i=0;i<n;i++){cow tmp;cin>>tmp.min_spf>>tmp.max_spf;c.push_back(tmp);}for(int i=0;i<m;i++){suns tmp;cin>>tmp.spf>>tmp.num;s.push_back(tmp);}sort(c.begin(),c.end(),cmp1);sort(s.begin(),s.end(),cmp2);int ans=0,cnt=0;for(int i=0;i<m;i++){while(cnt<n&&c[cnt].min_spf<=s[i].spf){qua.push(c[cnt].max_spf);cnt++;}while(s[i].num!=0&&!qua.empty()){int k=qua.top();qua.pop();if(k>=s[i].spf){ans++;s[i].num--;}}}cout<<ans<<endl; }?
總結
以上是生活随笔為你收集整理的POJ3614Sunscreen(优先队列+贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单计算器 逆波兰表达式
- 下一篇: yoyo思维题(困难) 组合数学