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. 编程内功修炼之数据结构—BTree(一)

    BTree,和二叉查找树和红黑树中一样,与关键字相联系的数据作为关键字存放在同一节点上. 一颗BTree树具有如下的特性:(根为root[T]) 1)每个节点x有以下域: (a)n[x],当前存储在节 ...

  2. 自定义的string类

    头文件Hi_String.h #include<iostream> #include<string.h> using namespace std; class Hi_Strin ...

  3. html天气预报小插件

    <head></head> <body> <iframe width="225" scrolling="no" hei ...

  4. poj 3616 Milking Time DP

    题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...

  5. Linux硬盘分区和格式化

    分区与格式化   先用fdisk分区,分区完成后再用mkfs格式化并创建文件系统,挂载,磁盘就能使用啦.   分区的原理:        MBR:主引导扇区 主分区表:64bytes,最多只能分四个主 ...

  6. python爬爬(网友提供学习)

    import urllib2,urllib,os,re def ZZ(url): pathw=os.getcwd() #图片和标题目录 imagetitleregion=r'<div class ...

  7. UIView 转 UIImage

    这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...

  8. python学习之路-10 网络编程之进阶

    本篇介绍内容 作用域 python类的多继承 IO多路复用 socketserver之源码剖析 多线程和多进程 作用域 if 1 == 1: name = "xxx" print( ...

  9. [Regular Expressions] Match the Start and End of a Line

    We can use: ^: match the beginning $: match the end Let's say we have the string like the following: ...

  10. Linux内核:sk_buff解析

    sk_buff 目录 1 sk_buff介绍 2 sk_buff组成 3 struct sk_buff 结构体 4 sk_buff成员变量 4.1 Layout布局 4.2 General通用 4.3 ...