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

    ZOJ Problem Set - 3410
    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
    

    Author: JIANG, Kai
    Contest: ZOJ Monthly, October 2010

    //优先队列和贪心的结合呀、

    //开始时、我是先对d进行升序排序,但是在处理t时,我的想法是判断这个陷阱能否解除,没有考虑怎样才扣HP最

    //少,所以WA了,正确思路是:排序后,依次加上ti,总的t大于di,就减去前面大的ti,这样扣得HP是最少的。

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    struct node
    {
        int t,d;
    };
    bool cmp(const node&a,const node&b)
    {
        return a.d<b.d;
    }
    node sa[25003];
    int main()
    {
        int N,K;
        int i,t,k;
        while(scanf("%d%d",&N,&K)!=EOF)
        {    priority_queue<int> Q;
              for(i=0;i<N;i++)
                scanf("%d%d",&sa[i].t,&sa[i].d);
              sort(sa,sa+N,cmp);
              t=0; k=K;
              for(i=0;i<N;i++)
              {
                t+=sa[i].t;
                Q.push(sa[i].t);
                while(t>sa[i].d)
                    {
                        t-=Q.top();
                        Q.pop();
                        k--;
                        if(k==0)
                         break;
                    }
                if(k==0)
                  break;
              }
            if(k==0)
               printf("-1\n");
            else
               printf("%d\n",K-k);

        }
        return 0;
    }

    #include <cstdio>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    struct node
    {
        int t,d;
    };
    bool cmp(const node&a,const node&b)
    {
        return a.d<b.d;
    }
    node sa[25003];
    int main()
    {
        int N,K;
        int i,t,k;
        while(scanf("%d%d",&N,&K)!=EOF)
        {    priority_queue<int> Q;
              for(i=0;i<N;i++)
                scanf("%d%d",&sa[i].t,&sa[i].d);
              sort(sa,sa+N,cmp);
              t=0; k=K;
              for(i=0;i<N;i++)
              {
                t+=sa[i].t;
                Q.push(sa[i].t);
                while(t>sa[i].d)
                    {
                        t-=Q.top();
                        Q.pop();
                        k--;
                        if(k==0)
                         break;
                    }
                if(k==0)
                  break;
              }
            if(k==0)
               printf("-1\n");
            else
               printf("%d\n",K-k);

        }
        return 0;
    }

  • 相关阅读:
    Net学习日记_SQL_1
    Net学习日记_基础提高_11_俄罗斯方块_代码篇
    Net学习日记_基础提高_11_俄罗斯方块_整理概括篇
    Net学习日记_基础提高_10
    C#抽象类和接口
    RSS大全
    如何使用deparam.js抓参数
    h5页面解决软键盘与100%X100%的页面的冲突
    29、数据库三大范式精要总结
    28、数据库三大范式精讲
  • 原文地址:https://www.cnblogs.com/372465774y/p/2582973.html
Copyright © 2011-2022 走看看