【优先队列+贪心】Expedition

Expedition
Time Limit: 1000MS   Memory Limit: 65536K
     

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.
 
题意:
   第一行,输入N,表示有N个加油站。
   第2~N+1行,每行输入两个数值L[i]和P[i],分别表示每个加油站的位置L[i]和最多能加P[i]升的汽油。
   第N+2行输入L和P,分别辆卡车要行驶L单位距离和卡车最开始时有P单位汽油。
   每向前行驶1单位距离消耗1单位汽油,起点,终点,以及加油站是在同一条直线上的,卡车的油箱无限大,无论加多少油都没问题。
        问小车从起点到终点最少要加几次油?若,在小车无法到达终点,则输出-1。
分析:
   起点和终点是一条直线,这条直线上有若干个加油站能够提供加油,最少需要加多少次油才能到达终点。
   要使得加油的次数最少的话,则是尽可能的让小车的没油的时候,才去加油站加汽油。
      对于小车来说,每次当经过第i个加油站的时候,能够选择加油或者不加,也就是在之后的路程中可以使用或者不使用这些汽油。
      所以,我们每次把经过的加油站时,所能够提供的每份汽油保存起来,在下次汽油不足的时候,才去取出来,每次先前保存下来的那份最多的汽油,再继续行驶,在判断。从而可以得出需要最少加油的次数。
  
代码:(优先队列+贪心)
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 struct Node{int L,P;};
 8 struct CMP_L{bool operator()(Node a,Node b){return a.L<b.L;}};
 9 struct CMP_P{bool operator()(Node a,Node b){return a.P<b.P;}};
10 int main()
11 {
12     int N,i,j,L,P,Begin,Dic,Times;
13     Node NUM;
14     while(scanf("%d",&N)!=EOF)
15     {
16         priority_queue<Node,vector<Node>,CMP_L>ID_L;
17         priority_queue<Node,vector<Node>,CMP_P>ID_P;
18         for(i=0;i<N;i++)
19         {
20             scanf("%d%d",&NUM.L,&NUM.P);
21             ID_L.push(NUM);
22         }
23         scanf("%d%d",&L,&P);
24         NUM.L=0;NUM.P=0;
25         ID_L.push(NUM);
26 
27         Begin=L;Times=0;
28         while(!ID_L.empty())
29         {
30             NUM=ID_L.top();ID_L.pop();
31             Dic=Begin-NUM.L;
32             while(P-Dic<0)
33             {
34                 if(ID_P.empty())
35                 {
36                     Times=-1;
37                     break;
38                 }
39                 P+=ID_P.top().P;
40                 ID_P.pop();
41                 Times++;
42             }
43             if(Times==-1)break;
44             P-=Dic;
45             Begin=NUM.L;
46             ID_P.push(NUM);
47         }
48         printf("%d\n",Times);
49     }
50     return 0;
51 }
View Code

 

posted @ 2015-07-30 01:31  Wurq  阅读(158)  评论(0编辑  收藏  举报