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



    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排序,如果有陷阱过不去,就去掉用时最长的那个陷阱。。。。。

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

    using namespace std;

    struct node
    {
        int t,d;
        bool operator<(const node a) const
        {
            return t<a.t;
        }
    }p[26000];

    bool cmp(node a,node  b)
    {
        return a.d<b.d;
    }

    int main()
    {
        int n,k;
    while(cin>>n>>k)
    {
        for(int i=0;i<n;i++)
        {
            int x,y;
            cin>>x>>y;
            p.t=x;   p.d=y;
        }
        sort(p,p+n,cmp);

        priority_queue<node> pq;

        int ddt=0;
        int ans=0;
        for(int i=0;i<n;i++)
        {
            pq.push(p);
            ddt+=p.t;
            if(ddt>p.d)
            {
                node x=pq.top();
                ddt-=x.t;
                pq.pop();
                ans++;
            }
        }

        ans=ans>=k? -1:ans;
        cout<<ans<<endl;
    }

        return 0;
    }


  • 相关阅读:
    程序员学习新技术的10个建议
    ES6 let和const总结归纳
    ES6 对象扩展运算符 res运算符
    ES6 变量的解构赋值
    ES6的开发环境搭建
    vue给同一元素绑定单击click和双击事件dblclick,执行不同逻辑
    "双非"应届生校招如何获得大厂青睐?(内附前端大厂面经+技术岗超全求职攻略)
    移动端300ms与点透总结
    Web移动端适配总结
    正则表达式总结
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350917.html
Copyright © 2011-2022 走看看