[优先队列+贪心]poj3614 Sunscreen

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值