1. 程式人生 > >poj 3614 抹防晒霜的牛(貪心 + 優先佇列)

poj 3614 抹防晒霜的牛(貪心 + 優先佇列)

題意:

有c只牛,l個瓶子。

每隻牛有一個防晒區間,minSpf 和 maxSpf,在這個區間內才能保持良好的防晒姿勢。

現在每個瓶子裡的防晒值是spf,然後每個瓶子能搞cover只牛。

問最多能夠讓幾頭牛保持良好的防晒姿勢。

解析:

莫名其妙就搞了一個早上。

另一道dp搞不出來。

這貪心是把maxSpf壓入優先隊列當中然後與牛的最小值比較就行了。

我開始的時候的貪心是用優先佇列儲存bottle,然後一個一個去試牛,wa了一個早上。

見程式碼:

ac:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 2500 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

struct Bottle
{
	int spf, cover;
	bool operator < (const Bottle a) const
	{
		return spf < a.spf;
	}
} bottle[maxn];

struct Cow
{
	int minSpf, maxSpf;
	bool operator < (const Cow a) const
	{
		return minSpf < a.minSpf;
	}
} cow[maxn];

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int c, l;
    while (~scanf("%d%d", &c, &l))
    {
    	for (int i = 0; i < c; i++)
    	{
    		scanf("%d%d", &cow[i].minSpf, &cow[i].maxSpf);
    	}
    	for (int i = 0; i < l; i++)
    	{
    		scanf("%d%d", &bottle[i].spf, &bottle[i].cover);
    	}
    	sort(bottle, bottle + l);
        sort(cow, cow + c);
        priority_queue<int, vector<int>, greater<int> > q;
        int j = 0, ans = 0;
        for (int i = 0; i < l; i++)
        {
            while (j < c && cow[j].minSpf <= bottle[i].spf)
            {
                q.push(cow[j].maxSpf);
                j++;
            }
            while (!q.empty() && bottle[i].cover)
            {
                int now = q.top();
                q.pop();
                if (now >= bottle[i].spf)
                {
                    ans++;
                    bottle[i].cover--;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

wa:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 2500 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

struct Bottle
{
	int spf, cover;
	bool operator < (const Bottle a) const
	{
		return a.spf < spf;
	}
} bottle[maxn];

struct Cow
{
	int minSpf, maxSpf;
	bool operator < (const Cow a) const
	{
		if (minSpf == a.minSpf)
		{
			return maxSpf < a.maxSpf;
		}
		return minSpf < a.minSpf;
	}
} cow[maxn];

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int c, l;
    while (~scanf("%d%d", &c, &l))
    {
        priority_queue<Bottle> q;
    	for (int i = 0; i < c; i++)
    	{
    		scanf("%d%d", &cow[i].minSpf, &cow[i].maxSpf);
    	}
    	for (int i = 0; i < l; i++)
    	{
    		scanf("%d%d", &bottle[i].spf, &bottle[i].cover);
    		q.push(bottle[i]);
    	}
    	sort(cow, cow + c);
        int index = 0;
        int ans = 0;
        while (!q.empty() && index < c)
        {
            Bottle now = q.top();
            q.pop();
            if (cow[index].minSpf < now.spf && now.spf < cow[index].maxSpf)
            {
                ans++;
                now.cover--;
                index++;
                if (now.cover != 0)
                    q.push(now);
//                cout << now.spf << " " << now.cover << endl;
//                cout << "ans:" << ans << endl;
            }
            else if (now.spf <= cow[index].minSpf)
            {
                continue;
            }
            else // cow[index].maxSpf < now.spf
            {
                index++;
                q.push(now);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}