Sunscreen POJ - 3614 (贪心 优先队列)

在这里插入图片描述

题解: 优先队列往往和贪心或dp结合到一起来用, 更重要的往往是算法策略, 优先队列只能用来优化局部.
这道题很明显的是贪心啦, 我们先将奶牛和防晒霜升序排列(sort默认pair使用first排序), 再满足minSPF的基础上, 我们尽可能的使用最小的maxSPF, 也就是物尽其用的原则, 更大的maxSPF也许有更多的选择, 很显然对于取maxSPF我们使用优先队列即可.

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 2510;

typedef pair<int, int> P;
P cow[maxn], bot[maxn];
priority_queue<int, vector<int>, greater<int> > q;
int main()
{
    int C, L;
    cin >> C >> L;
    for(int i = 1; i <= C; i++)
        cin >> cow[i].first >> cow[i].second;
    for(int i = 1; i <= L; i++)
        cin >> bot[i].first >> bot[i].second;

    sort(cow+1, cow+1+C);
    sort(bot+1, bot+1+L);
    int j = 1, ans = 0;
    for(int i = 1; i <= L; i++){
        //在满足minS的前提下, 选出最小的maxS
        while(j<=C && bot[i].first>=cow[j].first)
            q.push(cow[j++].second);
        while(!q.empty() && bot[i].second>0){
            int cur = q.top();
            q.pop();
            if(bot[i].first > cur) continue; //超过maxS
            ans++;
            bot[i].second--;
        }
    }
    cout << ans << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/87074331