您的位置:首页 > 其它

51Nod - 1246 贪心 + 优先队列

2017-02-03 10:30 211 查看

题意:

有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
输出能够获得的最高奖励。


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


Output示例
230


思路:

优先队列。思路和51Nod-1475一样,而且比1475简单,51Nod-1475题解传送门:点击打开链接
先按照时间排序,然后用优先队列维护最大值,始终保持时限晚的且价值更大的可以替代时限早的价值小的。

代码:

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

struct node {
int x, y;
bool operator < (const node &b) const {
return x < b.x;
}
}a[MAXN];

int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
sort (a + 1, a + 1 + n);
priority_queue <int, vector <int>, greater <int> > que;
int now = 0;
ll ans = 0;
for (int i = 1; i <= n; i++) {
int x = a[i].x, y = a[i].y;
if (x == now) {
if (y > que.top()) {
ans -= que.top(); que.pop();
ans += y; que.push(y);
}
}
if (now + 1 <= x) {
++now; ans += y;
que.push(y);
}
}
printf("%I64d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  51Nod acm 优先队列