zoukankan      html  css  js  c++  java
  • Eoj 1855. Expedition (贪心)

    题面

    单点时限: 2.0 sec

    内存限制: 256 MB

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck’s fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

    To repair the truck, the cows need to drive to the nearest town (no more than units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are fuel stops where the cows can stop to acquire additional fuel (.. units at each stop).

    The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently units away from the town and has units of fuel .

    Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

    输入格式
    Line : A single integer,
    Lines ..: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
    Line : Two space-separated integers, and
    输出格式
    A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output .

    思路

    每次我们都选择我们可以到达的所有加油站的最大者,用优先队列维护一下。

    代码实现

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    using namespace std;
    const int maxn=10010;
    struct node {
        int dis,flu;
    }e[maxn];
    int start,distancea;
    
    bool cmp (node a,node b) {
        return a.dis>b.dis;
    } 
    int main () {
        int n;
        cin>>n;
        for (int i=1;i<=n;i++) {
           int x,y;
           cin>>x>>y;
           node t={x,y};
           e[i]=t;
        }
        cin>>distancea>>start;
        sort (e+1,e+1+n,cmp);
        int cur=start;
        int cnt=0,k=1;
        priority_queue<int > q;
        if (start>=distancea) cout<<0<<endl;
        else {
           while (1) {
               if (cur>=distancea) break;
    
               for (int i=k;i<=n;i++) {
                   if (distancea-e[i].dis<=cur) {
                       q.push (e[i].flu);
                   }
                   else {
                       k=i;
                       break;
                   }
               }
               if (q.empty ()) {
                   cnt=-1;
                   break;
               }
               cur+=q.top ();
               q.pop ();
               cnt++;
           }
           
           cout<<cnt<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    ADB高级应用
    struts2 结合extjs实现的一个登录实例
    css3中关于伪类的使用
    漫谈并发编程(三):共享受限资源
    awk依照多个分隔符进行切割
    3星|《财经天下周刊》2017年21期:海外购几乎是亚马逊中国的最后一根救命稻草
    3星|《迷失的盛宴:中国保险产业1978-2014》:序言比正文精彩
    3星|《超级运营术》:互联网社区运营老手经验谈
    3星|《百年流水线》:流水线与工业、社会、艺术的交互史
    5星|戴蒙德《为什么有的国家富裕,有的国家贫穷》:为什么有的国家能发展出好制度
  • 原文地址:https://www.cnblogs.com/hhlya/p/13374718.html
Copyright © 2011-2022 走看看