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;
    }
    
  • 相关阅读:
    软件工程实验二
    软件工程实验一
    软件工程作业--ATM自助银行服务系统
    软件工程作业—举例分析流程图与活动图的区别与联系
    第一个随笔
    机器学习 实验四 决策树算法及应用
    机器学习实验三 朴素贝叶斯算法及应用
    机器学习 实验二 K-近邻算法及应用
    飞机订票系统(文档)
    机器学习 实验一 感知器及其应用
  • 原文地址:https://www.cnblogs.com/hhlya/p/13374718.html
Copyright © 2011-2022 走看看