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日 动态规划一 ...
随机推荐
- 查看nginx版本和安装的模块
查看nginx版本 # nginx -v nginx version: nginx/1.12.2 查看nginx配置了哪些模块 # nginx -V nginx version: nginx/1.12 ...
- 【转】Scala基础知识
原文地址.续 课程内容: 关于这节课 表达式 值 函数 类 继承 特质 类型 apply方法 单例对象 函数即对象 包 模式匹配 样本类 try-catch-finally 关于这节课 最初的几个星期 ...
- spring boot: 热部署(一) run as – java application (spring-loader-1.2.4.RELEASE.jar)
spring boot: 热部署(一) run as – java application (spring-loader-1.2.4.RELEASE.jar) 如果使用的run as – java a ...
- Arrays.copyof(···)与System.arraycopy(···)区别
首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明: public stati ...
- C#属性升级版--自动属性-chapter 3 P34-36
使用C#属性,能够通过将数据与它的设置和检索方法分离的方式公开类中的一段数据. 例如: namespace LanguageFeatures { public class Product { ...
- 折叠纸片PFold.js
PFold.js是一款折叠纸片插件,支持定义折叠纸牌数量.折叠动画效果.折叠方向,而且还支持折叠结束后回调方法. 在线实例 效果一 效果二 效果三 使用方法 <div id="uc-c ...
- BZOJ5090: [Lydsy1711月赛]组题(01分数规划)
5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 785 Solved: 186[Submit][Status ...
- 20155230 2016-2017-2 《Java程序设计》第八周学习总结
20155230 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logger ...
- mac终端下修改MySQL的编码格式--找不到my-default.cnf及my.cnf
首先请确认正确安装好MySQL. 1- 先配置环境变量path 1.1 打开终端,输入: cd ~ 会进入~文件夹, 1.2 然后输入:touch .bash_profile 回车执行后, 1.3 再 ...
- 【Xamarin】Visual Studio 2013 Xamarin for iOS 环境搭建
一.Mac安装Xamarin.iOS 1,我的Mac 环境:OSX 10.10.3.Xcode 6.3.2 (使用虚拟机亲测也成功 VMware 11 安装 Mac OS X10.10 ) Xam ...