zoj-3410-Layton's Escape
/*
ZOJ Problem Set - 3410Layton's Escape -------------------------------------------------------------------------------- Time Limit: 2 Seconds Memory Limit: 65536 KB -------------------------------------------------------------------------------- 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 2
40 60
60 90
80 120
2 1
30 120
60 40 Sample Output
1
-1 --------------------------------------------------------------------------------
Author: JIANG, Kai
Contest: ZOJ Monthly, October 2010 Submit Status
题意:Layton逃脱需要通过N个陷阱,对于i号陷阱,
可以选择花Ti时间移除,或者不移除而损失1点血,并且必须在时间Di内通过。
题目要求通过所有陷阱最少要损失多少血,如果不可能,输出-1。 解法:
其实本题解法和(上一题)古剑奇谭差不多。
该注意的是题目没有要求你按顺序经过哪个陷阱,所以要从小到大排序
贪心方法如下:按照Di从小到大对所有陷阱排序,
然后扫描处理,如果累计用时T≤Di,
那么什么也不需要损失血就可以移除所有陷阱,
继续处理下一个陷阱;否则,必然要损失血,
通过损失血来换取时间,
所以必然选择放弃移除花费时间最多的陷阱j,T-=Tj,HP–,直到有T≤Di。
*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 26000
struct trp
{
int ti;
int di;
bool operator < (const trp& a)const
{
return di<a.di;
}
} La[maxn];
int sum,hp,N,K,i;
int main()
{
while(~scanf("%d%d",&N,&K))
{
sum=;
hp=;
priority_queue<int> q;
for(i=; i<N; i++)
scanf("%d%d",&La[i].ti,&La[i].di);
sort(La,La+N);
for(i=; i<N; i++)
{
sum+=La[i].ti;
q.push(La[i].ti);
while(sum>La[i].di)
{
sum-=q.top();
hp++;
q.pop();
}
}
if(hp>=K)
printf("-1\n");
else
printf("%d\n",hp);
}
return ;
}
zoj-3410-Layton's Escape的更多相关文章
- zoj 3640 Help Me Escape (概率dp 递归求期望)
题目链接 Help Me Escape Time Limit: 2 Seconds Memory Limit: 32768 KB Background If thou doest w ...
- zoj 3640 Help Me Escape 概率DP
记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- ZOJ 3640 Help Me Escape:期望dp
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...
- ZOJ Monthly, October 2010 ABEFI
ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- 概率DP
POJ 3744 Scout YYF I 这就是一个乱搞题,暴力发现TLE了,然后看了看discuss里说可以矩阵加速,想了一会才想明白怎么用矩阵,分着算的啊.先算f[num[i]-1]之类的,代码太 ...
- 概率dp专场
专题链接 第一题--poj3744 Scout YYF I 链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
随机推荐
- 使用Bootstrap插件datapicker获取时间
引入css和js <link rel="stylesheet" href="/${appName}/commons/css/datapicker/datepicke ...
- E: 无法获得锁 /var/cache/apt/archives/lock - open (11 资源临时不可用)
事件: 今软件包有更新,进入更新管理器,点击安装更新,出现错误. 错误提示: E: 无法获得锁 /var/cache/apt/archives/lock - open (11 资源临时不可用) E: ...
- 转载 Linux top命令详解
TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止 ...
- vim中将tab 设置成4个空格
在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...
- js中字符串处理成数字的方法
<script> var a="11.1111"; var b="12.2222"; //第一种方法:乘以1的方法 //alert(a*1+b*1) ...
- 单链表是否有环的问题解决与讨论(java实现)
单链表是否有环的问题经常在面试中遇到,一般面试中会要求空间为O(1);再者求若有环,则求环产生时的起始位置. 下面采用java实现. //单链表class ListNode{ int val; Lis ...
- 如何在IDEA启动多个Spring Boot工程实例
在我讲解的案例中,经常一个工程启动多个实例,分别占用不同的端口,有很多读者百思不得其解,在博客上留言,给我发邮件,加我微信询问.所以有必要在博客上记录下,方便读者. step 1 在IDEA上点击Ap ...
- JDk和Mevan安装和配置
一.Mevan安装和配置 1.下载Maven 官方下载地址:http://maven.apache.org/download.html 选择你所希望下载的版本,并保存到常用安装目录.这里以Maven ...
- 基于Html5的爱情主题网站–表白神器
介绍 一个基于基于Html5的爱情主题,文字采用打字机效果,逐字打印,并带有键盘敲击声音.在chrome,safari,firefox,IE10下都有效,chrome下效果最佳.要注意的是safari ...
- 来来来,有讲一个吐血的故事(matlab)之脚本运行路径是什么
脚本运行路径是什么,这真是太重要!! 重要1:你默认保存的路径 重要2:你访问的相对路径 先放图: 再看一幅图: 我的操作,点击左侧的文件夹,使上框的显示栏路径不一样,再点击运行,发现pwd指示的路径 ...