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日 动态规划一 ...
随机推荐
- hdu 5701 中位数计数 思路题
中位数计数 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- [osg][osgearth]osg的分页加载,代码和结构图
DatabasePager加载数据的过程: 多线程 使用DatabasePager加载数据的流程: 左侧的图框表示数据的检索和输入, 中间的白色框表示用于数据存储的内存空间,而右边的图框表示存储数据的 ...
- PHP中exit()与die()的区别
PHP手册:die()Equivalent to exit(). 说明:die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别 ...
- 第108天:Ajax中XMLHttpRequest详解
在Ajax应用程序中,XmlHttpRequest对象负责将用户信息以异步通信地发送到服务器端,并接收服务器返回的响应信息和数据. XMLHttpRequest可以提供不重新加载页面的情况下更新网页, ...
- 牛客网——E进阶吧阶乘
链接:https://www.nowcoder.net/acm/contest/75/E来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32768K,其他语言65536K ...
- OC 数据持久化(数据本地化)- 本地存储
// // ViewController.m // IOS_0113_本地存储 // // Created by ma c on 16/1/13. // Copyright (c) 2016年 博文科 ...
- css之水平垂直居中方式
布局中常用到的水平垂直居中问题 作为一个前端开发人员,布局是我们日常工作中解除最多的,而水平垂直居中也必不可少的出现,面试中也经常遇到噢- 一.position:absolute(固定宽高) widt ...
- PHP与Java集成开发详解(一)
很久以前,有人从www上看到看到天空上一个很亮的亮点,它就是Java语言,与此同时,在另一个地方一位梦想家也看到了一个亮点,它就是PHP. 时间一天天过去,这两个亮点也变得越来越亮,很快,它们受到了编 ...
- 数据库 update select 多列操作
最常用的update语法是: UPDATE <table_name> SET <column_name1> = <value>, SET <column_ ...
- java面试题6
1.写一个冒泡排序的算法 升序排列: int[] nums = {5,6,9,10,20,30,28,27,15}; for(int i = 0;i<nums.length;i++){ for( ...