poj 3614

题意:一些奶牛要晒太阳,可是它们需要防晒霜。每头奶牛对防晒霜有一定的要求,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;;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值