/*
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. Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. S4 继承

    S3 系统是宽泛且灵活的,同类的 S3 对象也可能有不同的成员.但是,对于 S4 系统,就不会发生,也就是说,当我们创建一个属于某类的 S4 对象实例时,不能任意添加不在类表示中的字段.举个例子,在创 ...

  3. $state和$rootScope.$on($stateChangeStart)

    这两者的区别:请看博客:http://stackoverflow.com/questions/32680292/angularjs-state-and-rootscope-onstatechanges ...

  4. IntelliJ IDEA自定义类和方法注解模板

    现在Java开发主流工具应该是Intelij Idea 方便快捷. 本文将主要介绍如何用Intelij Idea配置类及方法的注释模板提高代码注释效率 1. 配置类注解模板 找到配置页面 File - ...

  5. poj3436网络流之最大流拆点

    这题看了半天看不懂题意...还是看的网上题意写的 加一个源点一个汇点,把每个点拆成两个,这两个点的流量是v,其他联通的边都设为无穷大 输入没有1的点就与源点连接,输出只有1的点就与汇点连接 还有这个输 ...

  6. 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮。

    package april; import java.util.ArrayList; import java.util.Scanner; /** * * @ClassName: Class_9 * @ ...

  7. Linux CentOS7.0下JAVA安装和配置环境变量

    一.前言: CentOS7.0虽然自带JDK1.7和1.8,运行“java -version”命令也可以看到版本信息,但是jdk的安装环境不全,比如缺少tool.jar和dt.jar等,这就导致“ja ...

  8. bzoj-4565-区间dp+状压

    4565: [Haoi2016]字符合并 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 253[Submit][Status ...

  9. 《剑指offer》习题解答(C/C++)

    1.二维数组中的查找 /* 题目:在一个二维数组中,没一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  10. 基于Open XML 导出数据到Excel

    数据导出的结果: 步骤1.新建一个Excel 文档,模板根据自己需要设置 步骤2.使用OpenXml  打开Excel 文件 步骤3.点击ReflectCode 功能,生成相应的代码文档 using ...