Layton'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
题意:Layton逃脱需要通过N个陷阱,对于i号陷阱,
可以选择花Ti时间移除,或者不移除而损失1点血,并且必须在时间Di内通过。
题目要求通过所有陷阱最少要损失多少血,如果血量小于0,输出-1。
思路:贪心:按照Di从小到大对所有陷阱排序(迫在眉睫的先搞)。并且用费一滴血的情况来替代最大移除陷阱的时间。
优先队列:保存最大移除陷阱的时间。
 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 400007
#define maxn 100007 using namespace std;
int n,k;
struct Trap
{
int t;
int d;
};
int cmp(Trap a,Trap b)
{
return a.d<b.d;
}
struct Temp
{
int value;
int pos;
};
Temp temp[maxm];
Trap trap[maxm];
int main()
{
while(~scanf("%d%d",&n,&k))
{
int num=;
for(int i=;i<n;i++)
{
scanf("%d%d",&trap[i].t,&trap[i].d);
}
sort(trap,trap+n,cmp);
int time=;
int cnt=;
priority_queue<int>q;
for(int i=;i<n;i++)
{
time+=trap[i].t;
q.push(trap[i].t);
if(time>trap[i].d)//当遇到要费一滴血的情况时,我们肯定要用这一滴血换取最大效益(即减去前面的最大时间)。
{
int p=q.top();
q.pop();
time-=p;
k--;
cnt++;
}
//q.push(trap[i].t); }
if(k>)
printf("%d\n",cnt);
else
printf("-1\n"); }
}

ZOJ-3410Layton's Escape(优先队列+贪心)的更多相关文章

  1. 最高的奖励 - 优先队列&贪心 / 并查集

    题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...

  2. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  3. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  5. poj2431(优先队列+贪心)

    题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...

  6. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

  7. 1350: To Add Which? (优先队列+贪心 或者 数组模拟)

    1350: To Add Which? Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitt ...

  8. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)

    链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...

  9. hdu 4544 优先队列+贪心

    题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...

随机推荐

  1. Java通过JNI调用C/C++

    From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...

  2. Find Successor & Predecessor in BST

    First, we use recursive way. Successor public class Solution { public TreeNode inorderSuccessor(Tree ...

  3. (转载) C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    转载http://blog.csdn.net/neo_ustc/article/details/9024839 有 些人写C/C++(以下假定为C++)程序,对unresolved external ...

  4. Ubuntu Eclipse的Tomcat小问题:不能输入server name,不能启动tomcat

    Ubuntu的Eclipse上安装Tomcat环境,这是让人烦啊,万幸还是终于解决了. Eclipse上Tomcat的搭建: 1.点击Eclipse上的菜单:Windows - Preference, ...

  5. add BOM to fix UTF-8 in Excel

    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

  6. Handsontable对单元格的操作

    1.自动填充单元格数据 fillHandle:true/false    //当值为true时,允许拖动单元格右下角,将其值自动填充到选中的单元格 2.合并单元格 mergeCells:[{row:起 ...

  7. (转)Tomcat 7 访问 Manager 和 Host Manager

    配置好 Tomcat 7.0 后,在 tomcat-users.xml 中配置用户角色来访问 localhost:8080 的这样三个按钮总出现问题: Server Status Manager Ap ...

  8. Html中value和name属性的作用

    1.按钮中用的value 指的是按钮上要显示的文本  比如“确定”“删除”等 2.复选框用的value 指的是这个复选框的值 3.单选框用的value 和复选框一样 4.下拉菜单用的value 是列表 ...

  9. DotDensityRenderer

    关键之处在于获取每个点所代表的的值 这里使用geodatabase类库中idatastatistic接口进行统计字段,再将结果传递给esrisysytem.istatisticsResult进行. 需 ...

  10. 前端--关于javascript基础

    首先javascript不是浏览器的附属品,只能说它大多数的运行环境是在浏览器中的,但又不仅仅局限于浏览器中.它是一门真正的程序设计语言,在这方面它和java.c.c++.c#是等同的,只不过它不直接 ...