poj 3614

題意:一些奶牛要曬太陽,可是它們需要防曬霜。每頭奶牛對防曬霜有一定的要求,SPF值有在MIN 和 MAX之間。給一些防曬霜的SPF和數量問,最多有幾頭奶牛能夠獲得想要的防曬霜。

怎樣選擇防曬霜呢。一款SPF儘量小防曬霜,應該給能使用的牛中,Max最小的牛用。因爲,我們要的是SPF儘量小的防曬霜,所以之後的SPF都會比這個要大,Max大的牛比小的牛更有機會能使用。對,就是貪心。

保持牛的Max儘量小,我們可以使用優先隊列。使得能夠滿足要求的牛中,第一個選到的就是Max最小牛。

#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 2500 + 5;
struct node
{
    int Min, Max;
    bool operator <(const node& a) const
    {
        if(Min != a.Min) return Min < a.Min;
        return Max < a.Max;
    }
};
struct node2
{
    int spf, num;
    bool operator <(const node2& a) const
    {
        return spf < a.spf;
    }
};
node cows[maxn];
node2 bot[maxn];
int main()
{
    int C, L;
    while(scanf("%d%d", &C, &L) == 2)
    {
        for(int i = 0; i < C; i++)
            scanf("%d%d", &cows[i].Min, &cows[i].Max);
        for(int i = 0; i < L; i++)
            scanf("%d%d", &bot[i].spf, &bot[i].num);
        sort(cows, cows + C);//排序
        sort(bot, bot + L);
        int cur = 0, ans = 0;
        priority_queue<int, vector<int>, greater<int> > q;
        for(int i = 0; i < L; i++)
        {
            while(cur < C && cows[cur].Min <= bot[i].spf)
            {
                q.push(cows[cur++].Max);//以牛的Max爲排序標準
            }
            while(!q.empty() && bot[i].num > 0)
            {
                int t = q.top(); q.pop();
                if(t >= bot[i].spf)//選擇滿足要求的牛,若不滿足要求,可以直接淘汰SPF只會越來越大
                {
                    bot[i].num--;
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章