51Nod 1163 最高的奖励 ( 贪心

1163 最高的奖励

Description

有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。

Input

第1行:一个数N,表示任务的数量(2 <= N <= 50000)
第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间E[i]以及对应的奖励W[i]。(1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)

Output

输出能够获得的最高奖励。

Sample Input

7
4 20
2 60
4 70
3 40
1 30
4 50
6 10

Sample Output

230

Hint

题意

中文题

1163 最高的奖励

题解:

建立一个以任务的奖金为权值的小根堆,初始堆空,用len记录堆的规模(堆中元素个数)。
对任务按截止时间 t[i] 从小到大排序,按序枚举每个任务,此时len有两种情况:
1len < t[i]选择该任务,将任务的奖金插入堆中。2.len>t[i],若该任务的奖金比堆中最小奖金值更大,则弹出堆顶元素,将该任务的奖金入堆。

AC代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const int mod = 1e9+7;
const int N = 1e5;
struct node {
    int t, c;
}p[N]; 
bool cmp (node x, node y)
{
    return x.t < y.t;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n; i++) scanf("%d%d",&p[i].t,&p[i].c);
    sort(p,p+n,cmp);
    priority_queue<int,vector<int>,greater<int> > fq;   //定义最小堆 
    ll sum = 0;
    for(int i = 0;i < n; i++) {
        int ans = p[i].c;
        if(p[i].t > fq.size()) {//如果堆内元素比p[i].t小 说明现在还可以加 
            sum += ans;
            fq.push(ans);
        }
        else {                 //堆内元素多 需要从里面找个最小的减去 
            sum += ans;
            fq.push(ans);
            int x = fq.top();
            sum -= x;
            fq.pop();
        }
    }
    printf("%lld\n",sum);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值