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 
#include 
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;
}

你可能感兴趣的:(Sunscreen)