/*
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的更多相关文章

  1. zoj 3640 Help Me Escape (概率dp 递归求期望)

    题目链接 Help Me Escape Time Limit: 2 Seconds      Memory Limit: 32768 KB Background     If thou doest w ...

  2. zoj 3640 Help Me Escape 概率DP

    记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  3. ZOJ 3640 Help Me Escape:期望dp

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...

  4. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

  5. ZOJ-3410Layton's Escape(优先队列+贪心)

    Layton's Escape Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Layton is a renowned arc ...

  6. 概率DP

    POJ 3744 Scout YYF I 这就是一个乱搞题,暴力发现TLE了,然后看了看discuss里说可以矩阵加速,想了一会才想明白怎么用矩阵,分着算的啊.先算f[num[i]-1]之类的,代码太 ...

  7. 概率dp专场

    专题链接 第一题--poj3744 Scout YYF I  链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...

  8. [转]概率DP总结 by kuangbin

    概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...

  9. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

随机推荐

  1. 在 php 中使用 strace、gdb、tcpdump 调试工具

    转自:http://www.syyong.com/php/Using-strace-GDB-and-tcpdump-debugging-tools-in-PHP.html 在 php 中我们最常使用调 ...

  2. SSH防止暴力破解--fail2ban

    一.ssh密钥对无交互登录 实战1:通过密钥进行sshd服务认证 服务端:linl_S    IP:10.0.0.15 客户端:lin_C    IP:10.0.0.16   1)在客户端生成密钥对 ...

  3. HDU 5816 状压DP&排列组合

    ---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  4. 个人知识管理系统Version1.0开发记录(10)

    物理分页 这次我们运用Mybatis拦截器来实现物理分页,后面会运用动态sql来实现,或者运用Map/CollectionUtils/StringUtils编写工具类来实现.oracle是运用的row ...

  5. 数论练习(5)——青蛙的约会(扩gcd)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 122502   Accepted: 26015 Descript ...

  6. 020PHP基础知识——函数(三)

    <?php /** * 任何数量的参数: * func_get_args() 接收一个数组,数组里面包含所有参数 * func_num_args() 取得共有几个参数 * func_get_ar ...

  7. hdu 6127 Hard challenge(极角/角度排序+枚举+结构体排序新写法)

    Hard challenge Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  8. [批处理]批量提取MKV资源

    最初是下了部没字幕的动漫,是720P MKV格式的,当时没注意,下完了以后才发现是没字幕的 后来去射手上找没有,百度了半天也没有 最后只能求救与已经下了这部动漫是MKV格式且是内挂字幕的人来帮忙 最后 ...

  9. SharePoint 2013的100个新功能之开发

    一:SharePoint应用 SharePoint 2013引入了云应用模型来供用户创建应用.SharePoint应用是独立的功能整合,扩展了SharePoint网站的功能.应用可以包含SharePo ...

  10. C++ 标准库和标准模板库(STL)

    转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 一.C++标准库 C++标准库的内容分为10类,分别是(建议在阅读中,将你已经用 ...