题意:一些奶牛要晒太阳,可是它们需要防晒霜。每头奶牛对防晒霜有一定的要求,SPF值有在MIN 和 MAX之间。给一些防晒霜的SPF和数量问,最多有几头奶牛能够获得想要的防晒霜。
怎样选择防晒霜呢。一款SPF尽量小防晒霜,应该给能使用的牛中,Max最小的牛用。因为,我们要的是SPF尽量小的防晒霜,所以之后的SPF都会比这个要大,Max大的牛比小的牛更有机会能使用。对,就是贪心。
保持牛的Max尽量小,我们可以使用优先队列。使得能够满足要求的牛中,第一个选到的就是Max最小牛。
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 2500 + 5;
struct node
{
int Min, Max;
bool operator <(const node& a) const
{
if(Min != a.Min) return Min < a.Min;
return Max < a.Max;
}
};
struct node2
{
int spf, num;
bool operator <(const node2& a) const
{
return spf < a.spf;
}
};
node cows[maxn];
node2 bot[maxn];
int main()
{
int C, L;
while(scanf("%d%d", &C, &L) == 2)
{
for(int i = 0; i < C; i++)
scanf("%d%d", &cows[i].Min, &cows[i].Max);
for(int i = 0; i < L; i++)
scanf("%d%d", &bot[i].spf, &bot[i].num);
sort(cows, cows + C);//排序
sort(bot, bot + L);
int cur = 0, ans = 0;
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 0; i < L; i++)
{
while(cur < C && cows[cur].Min <= bot[i].spf)
{
q.push(cows[cur++].Max);//以牛的Max为排序标准
}
while(!q.empty() && bot[i].num > 0)
{
int t = q.top(); q.pop();
if(t >= bot[i].spf)//选择满足要求的牛,若不满足要求,可以直接淘汰SPF只会越来越大
{
bot[i].num--;
ans++;
}
}
}
printf("%d\n", ans);
}
return 0;;
}