poj3614 Sunscreen(贪心)

Sunscreen

Description

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; minSPFi ≤ maxSPFi ≤ 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种防晒霜,第i头奶牛需要minSPF[i]和maxSPF[i]单位强度之间的防晒霜,第i种防晒霜强度为SPF[i],共有cover[i]瓶,求最多可满座多少奶牛进行日光浴。L,C<=2500

分析:将每头牛按minSPF递减排序,然后选择强度最大的防晒霜即可。

代码

#include <cstdio>
#include <algorithm>
#define N 5000
using namespace std;

struct cow
{
    int maxF,minF;
}a[N];
struct sunscreen
{
    int spf,cover;
}b[N];
int n,m;

int cmp(cow x, cow y) {return x.minF > y.minF;}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &a[i].minF, &a[i].maxF);
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &b[i].spf, &b[i].cover);
    sort(a+1,a+n+1,cmp);
    int cnt = 0,x;
    for (int i = 1; i <= n; i++)
    {
        int max = 0;
        bool fl = false;
        for (int j = 1; j <= m; j++)
            if (b[j].spf >= a[i].minF && b[j].spf <= a[i].maxF)
            {
                if (!b[j].cover) continue;
                fl = true;
                if (b[j].spf > max) 
                {
                    x = j;
                    max = b[j].spf;
                }
            }
        if (fl)
        {
            b[x].cover--;
            cnt++;
        }
    }
    printf("%d", cnt);
}

猜你喜欢

转载自blog.csdn.net/zhanghaoxian1/article/details/81662564