POJ 3416 Sunscreen(贪心)

Sunscreen
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5778 Accepted: 2022
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
Source

USACO 2007 November Gold

题意:c个奶牛晒太阳,每个奶牛有一个适宜的防晒值spf范围(如果它的防晒值太低,那么它会晒伤,如果这个值太高,它就不能沐浴阳光了……) 这里有L瓶防晒霜,可以将翻晒值控制在一个值。
现在需要给奶牛搽防晒霜,每个奶牛只搽一个防晒霜,问有多少个奶牛可以搽到防晒霜。
分析:
一开始虽然没有读懂题意但是还是用的贪心的思路,因为没有读懂题意不知道min有啥用=-=。知道之后其实思路还是一样的。
对于每一个防晒霜,找 上限大于它下限小于它 且上限尽量小的牛与它匹配。如果从小到大枚举防晒霜的spf值,它肯定要与上限尽量小的匹配更优(如果它能跟上限小一点的匹配,那么跟上限大一点且符合条件的匹配就没有和小的匹配更优。因为上限大的牛能匹配的范围更大所以尽量用来去匹配那些数值高的,能够实现个数最大化)这么说着好复杂 =-=。
想象一下,对于当前防晒霜,它的下一个的spf值是大于等于自己的。它和牛A和牛B都能匹配,但是牛B的上限更大一些,那它一定会和牛A匹配。因为它还要把机会留给下一个防晒霜啊=-=这样就说明了这个贪心的正确性

// Created by ZYD in 2015.
// Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
// long long s[300005];
struct type{
    int mi;
    int ma;
}spf[2505];
bool cmp(type x,type y){
    if(x.ma==y.ma) return x.mi>y.mi;
    return (x.ma<y.ma);
}
int c,l,x,y,ans=0;
// char s[200];
int n[2505],used[2505];
int main()
{
    //freopen("in.txt","r",stdin);
    mem(n);mem(used);
    scanf("%d %d\n",&c,&l);
    for(int i=1;i<=c;i++){
        scanf("%d %d",&spf[i].mi,&spf[i].ma);
        // if(cow[i]>ma) ma=cow[i];
    }
    sort(spf+1,spf+c+1,cmp);
    // for(int i=1;i<=c;i++) cout<<spf[i].ma<<endl;
    int m=0;
    for(int i=1;i<=l;i++)
    {
        scanf("%d %d\n",&x,&y);
        n[x]+=y;
        if(x>m) m=x; 
    }
    // sort(n+1,n+l+1);
    for(int i=1;i<=1000;i++)
    {
        for(int k=1;k<=n[i];k++)
            for(int j=1;j<=c;j++)
                if(used[j]==0 && spf[j].mi<=i && i<=spf[j].ma){
                    ans++;//cout<<i<<endl;
                    used[j]=1;
                    break;
                }
    }
    printf("%d\n",ans);
    return 0;

}

你可能感兴趣的:(poj)