Sunscreen

Sunscreen
描述
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 <= minSPF_i <= 1,000; minSPF_i <= maxSPF_i <= 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 SPF_i (1 <= SPF_i <= 1,000). Lotion bottle i can cover cover_i 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?
输入

  • Line 1: Two space-separated integers: C and L

  • Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPF_i and maxSPF_i

  • Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPF_i and cover_i
    输出
    A single line with an integer that is the maximum number of cows that can be protected while tanning
    样例输入
    3 2
    3 10
    2 5
    1 5
    6 2
    4 1
    样例输出
    2
    提示
    INPUT DETAILS:

3 cows; 2 lotions. Cows want SPF ratings of 3…10, 2…5, and 1…5. Lotions available: 6 (for two cows), 4 (for 1 cow). Cow 1 can use the SPF 6 lotion. Either cow 2 or cow 3 can use the SPF 4 lotion. Only 2 cows can be covered.

先说说这个题的题意,就是有一群牛要晒太阳(真舒服),但是他们还不能被晒伤,所以要涂抹防晒霜,再给你一些防晒霜,问你能有多少牛是合格的。
这个就是一个贪心的题,我刚开始就是从排序下手,先对牛进行排序,牛的承受能力从小到大排序,如果最小的能力一样,那么就对其进行最大的能力从小到大排序,为什么这样排序呢,因为最小的一样最大的越大,其承受能力的范围就越大,它相对于能够用的防晒霜的种类就可能多再对防晒霜排序,就是简单的从小到大排序,这样就能后实现小的防晒霜先用。这就是这个问题的贪心策略。

#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
    int x,y;
}a[2600],b[2600];
int cmp1(node a,node b)
{
    return a.y==b.y?a.x<b.x:a.y<b.y;
}
int cmp2(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i].x>>a[i].y;
    for(int i=1;i<=m;i++)
        cin>>b[i].x>>b[i].y;
    sort(a+1,a+n+1,cmp1);
    sort(b+1,b+m+1,cmp2);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(b[j].x>=a[i].x&&b[j].x<=a[i].y&&b[j].y>0)
            {
                b[j].y--;
                ans++;
                break;
            }
        }
    }
    cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值