1. 程式人生 > >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 ≤ 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
題意:奶牛由兩個防晒程度值minspf和maxspf,由L種spf不一樣的防晒霜各有一定數量的瓶數。
要求防晒霜的spf的值大於奶牛的minspf且小於maxspf,問這些瓶數的防晒霜能最多能保護多少奶牛。
思路:貪心,優先佇列
要想得最優數量,首先得進行排序
防晒霜比較好處理,按照spf值從小到大排序
奶牛排序時按照最大值maxspf從小到大排序,如果最大值maxspf相同則按照最小值minspf從小到大排序。
因為maxspf大的值對spf的選擇的餘地更多,所以先優先處理maxspf小的,整體上升序處理奶牛與防晒霜spf。

注意這裡每頭牛隻能使用一種防晒霜。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int x;
    int y;
}ci[2505],s[2505];
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 c,l;
    int m,n;
    scanf("%d %d",&c,&l);
    for(int i=0;i<c;i++)
        scanf("%d %d",&ci[i].x,&ci[i].y);
    for(int i=0;i<l;i++)
        scanf("%d %d",&s[i].x,&s[i].y);
    sort(ci,ci+c,cmp1);
    sort(s,s+l,cmp2);
    int cnt=0;
    for(int i=0;i<c;i++)
    {
        for(int j=0;j<l;j++)
        {
            if(s[j].x>=ci[i].x&&s[j].x<=ci[i].y&&s[j].y>0)
            {
                cnt++;
                s[j].y--;
                break;//一頭牛隻能用一種
            }
        }
    }
    printf("%d\n",cnt);
    return 0;
}