zoj 3410 Layton's Escape(贪心+优先队列)

解决一个关于教授莱顿从金字塔中逃脱的问题,使用贪心算法加优先队列的方法来最小化损失的生命值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开题目链接

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.

layton.jpg

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

题意:有一个人要通过金字塔,但是有很多陷阱,他必须过陷阱,每经过一个陷阱需要ti时间,但是后面还有一个怪物,在追着他们,如果总花费时间超过di,那么他们需要花费一点精力,当精力到0的时候,他们就死了,求这个人能不能通过金字塔。能通过是,最少需要多少精力。

贪心+优先队列,如果超过了时间,那么减掉最少的一定是最好的,这是需要用到优先队列。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

struct point
{
    long long t;
    long long d;
}p[25002];
bool cmp(point a,point b)
{
   if(a.d==b.d)
    return a.t<b.t;
   else
     return a.d<b.d;
}
int main()
{
   int i,j,n,k;
   while(~scanf("%d%d",&n,&k))
   {
       priority_queue<int>q;
       for(i=0;i<n;i++)
       {
           scanf("%lld%lld",&p[i].t,&p[i].d);
       }
       sort(p,p+n,cmp);

       long long T=0;
       int tt=0;
    for(i=0;i<n;i++)
     {
        T+=p[i].t;
        q.push(p[i].t);
        //cout<<T<<"&&&"<<endl;
         while(T>p[i].d)
           {
               tt++; T-=q.top();
              q.pop();
           }
         //cout<<tt<<"^^"<<endl;
     }
     if(tt>=k)
         printf("-1\n");
     else
         printf("%d\n",tt);

   }

    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值