Expedition(POJ 2431)

  • 原题如下:
    Expedition
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23033   Accepted: 6520

    Description

    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 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 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 L units away from the town and has P units of fuel (1 <= P <= 1,000,000). 

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

    Input

    * Line 1: A single integer, N 

    * Lines 2..N+1: 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 N+2: Two space-separated integers, L and P

    Output

    * Line 1: 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 -1.

    Sample Input

    4
    4 4
    5 2
    11 5
    15 10
    25 10
    

    Sample Output

    2
    

    Hint

    INPUT DETAILS: 

    The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively. 

    OUTPUT DETAILS: 

    Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
  • 分析:在开车开往终点的途中,只有在加油站才可以加油,但是我们也可以这样来理解这个事情:“在到达加油站i时,就获得了一次在之后的任何时候都可以加Bi单位汽油的权利”,在解决这道问题时,这两者应该时一样的,而在之后需要油的时候,就认为是在之前经过的加油站加的油就可以了。由于我们希望在到达终点时,加油次数尽可能地少,所以可以在每次燃料为0的时候进行加油,很显然,每次燃料为0需要加油的时候就找之前经过的并且还没加过油的能加油量Bi最大的加油站,选择从大到小取值的优先队列来维护可用的Bi即可:在经过加油站i时,往优先队列里加入Bi,当燃料箱空了时,如果优先队列也为空,则无法到达终点,否则取出优先队列中的最大元素,并用来给卡车加油。
  • 代码:
     1 #include <queue>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 struct node
     7 {
     8     int a;
     9     int b;
    10 };
    11 
    12 const int MAX_N=10100;
    13 int n;
    14 int l,p;
    15 node s[MAX_N+1];
    16 
    17 bool compare(const node &x, const node &y)
    18 {
    19     return x.a<y.a;
    20 }
    21 
    22 int main()
    23 {
    24     scanf("%d",&n);
    25     for (int i=0; i<n; i++)
    26     {
    27         scanf("%d %d", &s[i].a, &s[i].b);
    28     }
    29     scanf("%d %d", &l, &p);
    30     for (int i=0; i<n; i++) s[i].a=l-s[i].a;
    31     s[n].a=l;
    32     s[n].b=0;
    33     sort(s,s+(++n),compare);
    34     int ans=0,pos=0,tank=p;
    35     priority_queue<int> pque;
    36     for (int i=0; i<n; i++)
    37     {
    38         int d = s[i].a-pos;
    39         while (tank-d<0)
    40         {
    41             if (pque.empty())
    42             {
    43                 puts("-1");
    44                 return 0;
    45             }
    46             tank += pque.top();
    47             pque.pop();
    48             ans++;
    49         }
    50         tank -= d;
    51         pos=s[i].a;
    52         pque.push(s[i].b); 
    53     }
    54     printf("%d\n",ans);
    55 }

猜你喜欢

转载自www.cnblogs.com/Ymir-TaoMee/p/9444235.html