POJ 3614 贪心

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

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; minSPFimaxSPFi ≤ 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

题目大意: 给出C头牛对阳光的最小忍受程度和最大忍受程度,( 大概就这么理解吧)给出L种防晒霜的防晒效果和瓶数, 一瓶防晒霜只能用在一头牛身上, 且防晒效果位于该牛对阳光的最小忍受程度和最大忍受程度之间才认为是有效的。 求满足这个条件的最多的奶牛数量。

思路: 贪心, 防晒霜按效果从低到高排序; 效果低的防晒霜, 在牛的最小忍受程度小于它且最大忍受程度大于它的情况下,我们肯定更希望这瓶防晒霜用在最大忍受程度最小的牛身上。因为往后还有效果高的防晒霜, 可以处理最大忍受程度较大的情况。 这样贪是正确的, 即优先对上限值进行排序。 反之很容易举出反例, 比如:

蓝线是牛对阳光的忍受程度, 红线是防晒霜的效果。(假设每种都只有一瓶)如图是按照最低忍受程度排序的结果,我们可以看到效果最差的防晒霜用于第一头牛, 那么会造成效果好的防晒霜无法使用。 但是按照最大忍受程度排序的话, 就不会有这种问题。思路大概就是这样,用优先队列也可以做,这里用的是结构体,定义了两种cmp函数,分别适用于奶牛和防晒霜。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

struct node
{
    int l,r;
};

bool cmp1(node a,node b)    //奶牛
{
    if(a.r==b.r)
        return a.l<b.l;
    return a.r<b.r;
}

bool cmp2(node a,node b)    //防晒霜
{
    return a.l<b.l;
}

node a[2505];
node b[2505];

int main()
{
    int c,l;
    scanf("%d %d",&c,&l);
    for(int i=0;i<c;i++)
        scanf("%d %d",&a[i].l,&a[i].r);
    for(int i=0;i<l;i++)
        scanf("%d %d",&b[i].l,&b[i].r);
    sort(a,a+c,cmp1);
    sort(b,b+l,cmp2);
    int cnt=0;
    for(int i=0;i<c;i++)
    {
        for(int j=0;j<l;j++)
        {
            if(b[j].r==0)//该种防晒霜用完了
                continue;
            if(a[i].l>b[j].l)//效果小于最低忍耐程度
                continue;
            if(a[i].r<b[j].l)//效果高于最大忍耐程度 break是因为防晒霜效果是升序排列的
                break;
            --b[j].r;//用了一瓶
            ++cnt;  //奶牛头数
            break;
        }
    }
    printf("%d\n",cnt);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值