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
晒太阳与涂防晒霜的故事- -,每一头牛有一个承受的spf的上限和下限,求给定的防晒霜(spf值和涂的数量)最多可以给几头牛涂。
知道了时贪心还是不会做,简直太水le。。。。。。。
因为同时有上限和下限约束着防晒霜的使用,所以贪心策略不一样。需要先去掉一个约束再执行贪心策略,所以从小打到枚举每一种防晒霜,对于每一种防晒霜,我们需要先找出所有spf下限小于它 的牛,然后再贪心选出可以使用防晒霜的spf上限最小的牛,就能得到最优解了。用了优先队列实现。
#include<string.h>
#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,t;
struct pp{
int down, up;
bool operator<(const pp &b)const{
return up>b.up;
}
}p[2505],q[2505];
priority_queue<pp> que;
bool cmp1(pp a,pp b){
return a.down<b.down;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d%d",&p[i].down,&p[i].up);
}
for(int i=0;i<m;i++){
scanf("%d%d",&q[i].down,&q[i].up);
}
sort(p,p+n,cmp1);
sort(q,q+m,cmp1);
int x=0;
int ans=0;
for(int i=0;i<m;i++){
while(x<n&&p[x].down<=q[i].down){
que.push(p[x]);
x++;
}
while(q[i].up>0&&!que.empty()){
int cnt=que.top().up;que.pop();
if(cnt>=q[i].down){
ans++;
q[i].up--;
}
}
}
printf("%d",ans);
return 0;
}