51NOD 1163 最高的奖励

来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163

这个题 自己想了想 mmp 感觉一做贪心题只会用 sort 忽略了 优先队列

这题搜了题解后 大概明白了  就是建立一个最小堆  把cost 压入最小堆 如果当前时间 》 Q.size() 说明可以直接加

如果小于等于 就要把cost 压入后 取一个最小的出来 

挺好的一个题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 50000+10;

struct node
{
    int l,cost;
    bool operator <(const node & a)const{
        return l < a.l;
    }
}s[maxn];

int main ()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d",&s[i].l,&s[i].cost);
    sort(s,s+n);
    priority_queue<int,vector<int>,greater<int> > Q;
    ll res = 0;
    for(int i=0;i<n;i++)
    {
        if(s[i].l > Q.size()){
            res += s[i].cost;
            Q.push(s[i].cost);
        }
        else
        {
            res += s[i].cost;
            Q.push(s[i].cost);
            int t = Q.top();
            Q.pop();
            res -= t;
        }
    }
    printf("%lld
",res);
}
原文地址:https://www.cnblogs.com/Draymonder/p/7352731.html