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: minSPFiand 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
题意:
- 有C头奶牛要去沐光浴,太阳光太强烈会晒坏皮肤,太弱又会没效果。
- 每头牛都有一个太阳光适宜的范围经行沐光浴,分别给出minspf_i和maxspf_i。
- 有L种防晒霜,每种防晒霜可以把所受阳光固定于一个值spf_i,每种有cover_i瓶。
- 问最多会有几头牛得到合适的光晒强度?
思路:
- 那么将奶牛按照阳光强度的最小值从小到大排序。
- 将防晒霜也按照能固定的阳光强度从小到大排序
- 枚举防晒霜,将minspf<=spf的奶牛的maxspf存到优先队列中,
- 然后值小的先出队列,看是否满足maxspf>=spf,更新记录值。
#include <iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std;
const int maxn=100001;
pair<int,int>cow[maxn];
pair<int,int>bottle[maxn];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
int c,l;
scanf("%d%d",&c,&l);
for(int i=0; i<c; i++)
{
scanf("%d%d",&cow[i].first,&cow[i].second);
}
for(int i=0; i<l; i++)
{
scanf("%d%d",&bottle[i].first,&bottle[i].second);
}
sort(cow,cow+c);
sort(bottle,bottle+l);
int cur=0;// 现在正等待涂防晒霜的奶牛的index
int result=0;
for(int i=0; i<l; i++)
{
while(cur<c&&cow[cur].first<=bottle[i].first)
{
q.push(cow[cur].second);
++cur;
}
while(!q.empty()&&bottle[i].second)
{
int maxSPF=q.top();
q.pop();// “奶牛上限”比这一瓶的上限大,说明这头奶牛可以被涂上防晒霜
if(maxSPF>=bottle[i].first)
{
++result;
--bottle[i].second;
}
// else 这头奶牛不能被涂上,因为bottle是按SPF排过序的,没有比这瓶更小的SPF了
}
}
cout<<result<<endl;
return 0;
}