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"); }
}