51nod 1163 最高的奖励

来源:互联网 发布:义阳博弈量能 知乎 编辑:程序博客网 时间:2024/05/23 02:05
有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。
Input
第1行:一个数N,表示任务的数量(2 <= N <= 50000) 
第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间Eii以及对应的奖励Wii。(1 <= Eii <= 10^9,1 <= Wii <= 10^9)
Output
输出能够获得的最高奖励。
Sample Input
7
4 20
2 60
4 70
3 40
1 30
4 50
6 10
Sample Output

230

按照完成的最后时间升序排列每个任务,从左向右遍历,如果当前任务还有时间去完成就直接扔到堆里,如果当前任务没有时间去做,就把当前任务能获得的奖励与堆顶比较,比堆顶大就把堆顶pop,把当前任务push。

#include<queue>#include<cstdio>#include<algorithm>#include<vector>using namespace std;const int M = 5e4 + 5;struct Node{    int t, w;}a[M];priority_queue<int, vector<int>, greater<int> > q;bool cmp(Node a, Node b){    return a.t < b.t;}int main(){    int n, tim=1, tmp;    long long ans=0;    scanf("%d", &n);    for(int i=1;i<=n;i++)        scanf("%d%d", &a[i].t, &a[i].w);    sort(a+1, a+n+1, cmp);    for(int i=1;i<=n;i++)    {        if(tim<=a[i].t)        {            q.push(a[i].w);            tim++;            ans += a[i].w;        }        else        {            tmp = q.top();            if(tmp<a[i].w)            {                q.pop();                ans -= tmp;                q.push(a[i].w);                ans += a[i].w;            }        }    }    printf("%lld", ans);    return 0;}


原创粉丝点击