51nod 1163:最高的奖励 优先队列
2015-10-21 22:44
423 查看
1163 最高的奖励
![](http://file.51nod.com/images/icon/ok.png)
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
![](http://file.51nod.com/images/icon/star.png)
收藏
![](http://file.51nod.com/images/icon/plus.png)
关注
有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。
Input
Output
Input示例
Output示例
也算是慢慢理解优先队列的意义了,就是先把符合条件的存进去,这个队列满足于存储的元素也能符合接下来的每轮条件,然后弹出最优的值。
代码:
![](http://file.51nod.com/images/icon/ok.png)
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
![](http://file.51nod.com/images/icon/star.png)
收藏
![](http://file.51nod.com/images/icon/plus.png)
关注
有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
也算是慢慢理解优先队列的意义了,就是先把符合条件的存进去,这个队列满足于存储的元素也能符合接下来的每轮条件,然后弹出最优的值。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <queue> #pragma warning(disable:4996) using namespace std; int n; struct no { int val; int end_time; friend bool operator<(no n1, no n2) { return n1.val < n2.val; } }node[50005]; bool cmp(no n1, no n2) { if (n1.end_time == n2.end_time) return n1.val > n2.val; else return n1.end_time > n2.end_time; } int main() { //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int i, j, temp1, temp2; long long res; priority_queue<no>q; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &temp1, &temp2); node[i].end_time = min(n, temp1); node[i].val = temp2; } sort(node, node + n, cmp); res = 0; j = 0; for (i = n; i >= 1; i--) { while (node[j].end_time >= i&&j < n) { q.push(node[j]); j++; } if (!q.empty()) { no ntemp = q.top(); q.pop(); res = res + ntemp.val; } } printf("%lld\n", res); //system("pause"); return 0; }
相关文章推荐
- 2015南阳CCPC G - Ancient Go 暴力
- Redis部署管理持久化迁移
- 51nod 1163:最高的奖励 优先队列
- Java开源的支持xpath的html解析器介绍--JsoupXpath
- Net-SNMP动态库方式扩展MIB
- ORA-02020的处理,dblink超出数量的设置
- C++中指针和引用的区别(转)
- python 发送邮件,未完
- 杭电acm2029-Palindromes _easy version
- STL学习笔记-quene容器(队列)
- C语言,内存管理
- 边缘检测
- 两步让你的mobile traffic通过fiddler代理传送
- ORA-01033: ORACLE initialization or shutdown in progress数据文件损坏错误解决方案
- 继承与构造方法~继续话痨~~
- 第 三 十 七 天:增 加 inode 和 挂 载 ios 镜 像
- 通过Java插入MYSQL的DATETIME类型
- Python爬虫----抓取豆瓣电影Top250
- sicily 1029. Rabbit
- morris算法-----高级二叉树遍历算法