zoukankan      html  css  js  c++  java
  • zoj-3410-Layton's Escape

    /*
    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=0;
            hp=0;
            priority_queue<int> q;
            for(i=0; i<N; i++)
                scanf("%d%d",&La[i].ti,&La[i].di);
            sort(La,La+N);
            for(i=0; 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
    ");
            else
                printf("%d
    ",hp);
        }
        return 0;
    }
  • 相关阅读:
    日志到底该如何打印?
    消息摘要(MessageDigest)
    URL编码及解码原理
    AES简介
    MySQL中整型长度的意义
    keytool生成密钥
    证书类型及秘钥库
    Base64编码及解码原理
    Holistically-Nested Edge Detection 论文总结
    全新bili主题
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3227247.html
Copyright © 2011-2022 走看看