POJ 3614 Sunscreen(貪心+優先佇列)
Time Limit:1000MS |
Memory Limit:65536K |
Total Submissions:5713 |
Accepted:1994 |
Description
To avoid unsightly burns while tanning, each of theC(1 ≤C≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cowihas
a minimum and maximumSPFrating (1 ≤minSPFi≤ 1,000;minSPFi
The cows have a picnic basket withL(1 ≤L≤ 2500) bottles of sunscreen lotion, each bottleiwith anSPFratingSPFi(1
≤SPFi≤ 1,000). Lotion bottleican covercoveri
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
Input
* Line 1: Two space-separated integers:CandL
* Lines 2..C+1: Lineidescribes cowi's lotion requires with two integers:minSPFiandmaxSPFi
* LinesC+2..C+L+1: Linei+C+1 describes a sunscreen lotion bottleiwith space-separated integers:SPFiandcoveri
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 1
Sample Output
2
題意:有C頭奶牛要去沐光浴,太陽光太強烈會晒壞面板,太弱又會沒效果。每頭牛都有一個太陽光適宜的範圍經行沐光浴,分別給出minspf_i和maxspf_i。 有L種防晒霜,每種防晒霜可以把所受陽光固定於一個值spf_i,每種有cover_i瓶。 問最多會有幾頭牛得到合適的光晒強度?
題解:woc,USACO的牛生活真是豐富多彩啊(;′⌒`) 貪心策略,在滿足minspf的條件下,儘量將spf的防晒霜塗到maxspf小的奶牛身上,因為maxspf大的奶牛有更多的選擇。這裡就需要一個優先佇列來儲存滿足minspf的奶牛的maxspf的值。 具體解題步驟如下:
1.將奶牛按照minspf升序排列,將防晒霜按照spf升序排列。
2.列舉防晒霜,將minspf<=spf的奶牛的maxspf存到優先佇列中,然後值小的先出佇列,看是否滿足maxspf>=spf,更新記錄值。
程式碼如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node1
{
int minspf,maxspf;
}cow[2510];
struct node2
{
int spf,num;
}lotion[2510];
int cmp1(node1 a,node1 b)
{
return a.minspf<b.minspf;
}
int cmp2(node2 a,node2 b)
{
return a.spf<b.spf;
}
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;++i)
scanf("%d%d",&cow[i].minspf,&cow[i].maxspf);
for(i=0;i<m;++i)
scanf("%d%d",&lotion[i].spf,&lotion[i].num);
sort(cow,cow+n,cmp1);
sort(lotion,lotion+m,cmp2);
priority_queue<int, vector<int>,greater<int> >q;//維護被選中奶牛的maxspf
j=0;
int ans=0;
for(i=0;i<m;++i)
{
while(j<n&&cow[j].minspf<=lotion[i].spf)
{
q.push(cow[j].maxspf);
j++;
}
while(!q.empty()&&lotion[i].num)
{
int cnt=q.top();
q.pop();
if(cnt>=lotion[i].spf)
{
ans++;
lotion[i].num--;
}
}
}
printf("%d\n",ans);
}
return 0;
}