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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值