zoj 3410 Layton's Escape(贪心+优先队列)
来源:互联网 发布:三维模型设计软件 编辑:程序博客网 时间:2024/06/03 22:50
点击打开题目链接
Professor Layton is a renowned archaeologist from London's Gressenheller University. He and his apprentice Luke has solved various mysteries in different places.
Unfortunately, Layton and Luke are trapped in a pyramid now. To escape from this dangerous place, they need to pass N traps. For each trap, they can use Ti minutes to remove it. If they pass an unremoved trap, they will lose 1 HP. They have K HP at the beginning of the escape and they will die at 0 HP.
Of course, they don't want trigger any traps, but there is a monster chasing them. If they haven't pass the ith trap in Di minutes, the monster will catch and eat them. The time they start to escape is 0, and the time cost on running will be ignored. Please help Layton to escape from the pyramid with the minimal HP cost.
Input
There are multiple test cases (no more than 20).
For each test case, the first line contains two integers N and K (1 <= N <= 25000, 1 <= K <= 5000), then followed by N lines, the ith line contains two integers Ti and Di (0 <= Ti <= 10^9, 0 <= Di <= 10^9).
Output
For each test case, if they can escape from the pyramid, output the minimal HP cost, otherwise output -1.
Sample Input
3 240 6060 9080 1202 130 12060 40
Sample Output
1-1
贪心+优先队列,如果超过了时间,那么减掉最少的一定是最好的,这是需要用到优先队列。
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;struct point{ long long t; long long d;}p[25002];bool cmp(point a,point b){ if(a.d==b.d) return a.t<b.t; else return a.d<b.d;}int main(){ int i,j,n,k; while(~scanf("%d%d",&n,&k)) { priority_queue<int>q; for(i=0;i<n;i++) { scanf("%lld%lld",&p[i].t,&p[i].d); } sort(p,p+n,cmp); long long T=0; int tt=0; for(i=0;i<n;i++) { T+=p[i].t; q.push(p[i].t); //cout<<T<<"&&&"<<endl; while(T>p[i].d) { tt++; T-=q.top(); q.pop(); } //cout<<tt<<"^^"<<endl; } if(tt>=k) printf("-1\n"); else printf("%d\n",tt); } return 0;}
- zoj 3410 Layton's Escape(优先队列+贪心)
- zoj 3410 Layton's Escape(贪心+优先队列)
- zoj3410 Layton's Escape (优先队列)
- zoj 3410 Layton's Escape
- ZOJ-3410-Layton's Escape_贪心
- ZOJ3410--Layton's Escape
- ZOJ 3953 贪心+优先队列
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
- ZOJ 3916 Buy Cakes (贪心+优先队列维护)
- poj1042(贪心+优先队列)
- POJ1456Supermarket(贪心+优先队列)
- poj3465(贪心+优先队列)
- poj2431Expedition(贪心,优先队列)
- 推销员(优先队列+贪心)
- poj3190(贪心,优先队列)
- Sunscreen (贪心+优先队列)
- ZOJ 3447 Doraemon's Number Game(优先队列+高精度运算)
- 文章标题 CSU 1815 : Enterprising Escape(BFS--优先队列)
- VC2010 CString.Format使用报错 error C2664
- Hadoop自学笔记(七)Hadoop环境配置和优化
- 祁县财政局与杨占全、程秋转、杨继明、程国俭、杨惠丽借款合同纠纷一审民事判决书
- UVA 357 Let Me Count The Ways 完全背包
- MAC OS 暗黑世界服务器端环境搭建(适合用MAC系统的亲们)
- zoj 3410 Layton's Escape(贪心+优先队列)
- Linux 学习之路(一)——Linux 发行版本的选择以及opensuse 13.1 安装
- FIFO存储深度(容量)计算
- C#对象克隆的战争
- javascript页面动态显示时间变化
- 修改论文格式的方法总结
- DOM4J 学习总结之 图示
- 动态代理
- 黑马程序员——Java基础---多线程