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;
    }
    
  • 相关阅读:
    结对项目--四则运算“软件”之升级版
    个人项目--多元四则运算
    《构建之法》1-5章后感
    git bash的安装与配置
    随笔
    numpy数组及处理:效率对比
    完整的中英文词频统计
    组合数据类型,英文词频统计
    9.13作业2(完整温度转换、数字游戏、解析身份证号...)
    Mad Libs游戏,华氏与摄氏转换
  • 原文地址:https://www.cnblogs.com/hhlya/p/13374718.html
Copyright © 2011-2022 走看看