[优先队列+贪心]poj3614 Sunscreen

来源:互联网 发布:redis mysql同步 编辑:程序博客网 时间:2024/06/06 12:53

http://poj.org/problem?id=3614

那么将奶牛按照阳光强度的最小值从小到大排序。
将防晒霜也按照能固定的阳光强度从小到大排序

从最小的防晒霜枚举,将所有符合 最小值小于等于该防晒霜的 奶牛的 最大值 放入优先队列之中。
然后优先队列是小值先出
所以就可以将这些最大值中的最小的取出来。更新答案。

#include <iostream>#include<algorithm>#include<queue>#include<math.h>#include<stdio.h>#include<string.h>const int INF=0x3f3f3f3f;using namespace std;const int MAXN = 10000;int C,L;typedef pair<int,int> P;priority_queue<int,vector<int>,greater<int> > q;P cow[MAXN],bot[MAXN];bool cmp(P a,P b){    return a.first < b.first;}int main(){    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",&bot[i].first,&bot[i].second);    sort(cow,cow+C,cmp);    sort(bot,bot+L,cmp);    int j = 0,ans = 0;    for(int i = 0; i < L;i++)    {        while( j < C && cow[j].first <=bot[i].first)        {            q.push(cow[j].second);            j++;        }        while(!q.empty() && bot[i].second)        {            int a = q.top();            q.pop();            if (a<bot[i].first) continue;            ans++;            bot[i].second--;        }    }    printf("%d\n",ans);    return 0;}
0 0
原创粉丝点击