POJ 3614 - Sunscreen
题目链接:http://poj.org/problem?id=3614
Time Limit: 1000MS Memory Limit: 65536K
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 1
Sample Output
2
把牛按minSPF从小到大排序一下,防晒霜按SPF排序一下。
然后遍历防晒霜lontion[1...L],对于lotion[i],把所有满足minSPF小于等于lontion[i].SPF的牛都找出来,入队。
我们将这个队列定义为按cow[i].maxSPF的从小到大排序的优先队列,那么,每次取出的队首,都是当前要求最苛刻(SPF要求范围最小的)那头牛,先满足这些牛,如果当前这 种防晒霜能满足这头牛,就用掉一瓶,否则就放弃这头牛。
反复如此,直到所有防晒霜用完。
1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 using namespace std; 5 struct Cows{ 6 int minSPF,maxSPF; 7 bool operator<(const Cows &oth)const 8 { 9 return maxSPF>oth.maxSPF; 10 } 11 }cow[2505]; 12 struct Lotions{ 13 int SPF,cover; 14 }lotion[2505]; 15 bool cmp1(Lotions a,Lotions b) {return a.SPF<b.SPF;} 16 bool cmp2(Cows a,Cows b) {return a.minSPF<b.minSPF;} 17 int main() 18 { 19 int C,L; 20 priority_queue<Cows> q; 21 scanf("%d%d",&C,&L); 22 for(int i=1;i<=C;i++) scanf("%d%d",&cow[i].minSPF,&cow[i].maxSPF); 23 for(int i=1;i<=L;i++) scanf("%d%d",&lotion[i].SPF,&lotion[i].cover); 24 sort(lotion+1,lotion+L+1,cmp1); 25 sort(cow+1,cow+C+1,cmp2); 26 int now=1,ans=0; 27 for(int i=1;i<=L;i++) 28 { 29 while(now <= C && cow[now].minSPF <= lotion[i].SPF)//由于事先排过序,所以当前这头一旦不满足cow[now].minSPF <= lotion[i].SPF,之后的牛都不满足 30 { 31 q.push(cow[now]); 32 now++; 33 } 34 while(!q.empty() && lotion[i].cover>0)//要么是当前这种防晒霜很多,把所有牛都涂了;要么就是当前这种防晒霜用完了 35 { 36 if(q.top().maxSPF >= lotion[i].SPF)//这种防晒霜可以给这头牛涂 37 { 38 ans++;//能涂的牛的数量增加1 39 lotion[i].cover--;//当前这种防晒霜用去一瓶 40 } 41 q.pop();//涂的上就涂,涂不上就放弃 42 } 43 } 44 printf("%d\n",ans); 45 }