Title Link: http://poj.org/problem?id=2431
Expedition
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9148 |
|
Accepted: 2673 |
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, Windin G Road. On this road, between the town and the current location of the truck, there is n (1 <= n <=) fuel stops wher E The cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and was especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the the-the-the-town. Fortunately, the capacity of the fuel tank on their truck are so large that there are effectively no limit to the amount of Fuel it can hold. The truck is currently L units away from the town and have 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, 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 isn't possible to reach of the town, Output-1.
Sample Input
44 45 211 515 1025 10
Sample Output
2
Hint
INPUT DETAILS:
The truck is a units away from the town; The truck has ten units of fuel. Along the road, there is 4 fuel stops at distances 4, 5, one, and (so these is initially at distances 21 , +, + from the truck). These fuel stops can supply up to 4, 2, 5, and ten units of fuel, respectively.
OUTPUT DETAILS:
Drive ten units, stop to acquire ten more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then driv E to the town.
This is the Challenge Program design contest on the heap and the use of priority queue examples, the book gives the idea is very ingenious
Because it is the best solution, we need as few gas stations as possible, so every time we want to add the largest one when refueling, so the gas station push into the priority_queue (heap structure, the default maximum value per Pop)
From the beginning to run, the journey through the gas station into several points, each part as long as through refueling can reach the gas station can, therefore, the end also as a gas station distance l, refueling volume 0;
#include <cstdio>//input is large, so C-style input and output, AC time 16ms, all into C + + style needs 313ms#include<queue> #include < Algorithm>using namespace Std;struct node{int a1,b1;}; int cmp (const node& A,const node& b) {return a.a1<b.a1;} int main () {priority_queue<int> pq;int n;node no[10010];while (scanf ("%d", &n)!=eof) {for (int i=0;i<n;i++) {scanf ("%d%d", &NO[I].A1,&NO[I].B1);} int l,p,cnt=0,sign=0;scanf ("%d%d", &l,&p); for (int i=0;i<n;i++) { //The A1 that the topic requires is the distance from the current to the end point, and we convert it to the distance from the starting point no[ I].A1=L-NO[I].A1;} Sort (no,no+n,cmp); int Index=0;//pq.push (P); int pos=0;no[n].a1=l; Set the end point to gas station no[n].b1=0;n++;for (int i=0;i<n;i++) {int d=no[i].a1-pos;//The distance currently required to go while (p<d) { if (Pq.empty ()) { The fuel tank is empty puts ("-1"); Sign=1; Break } else{P+=pq.top (); Pq.pop (); cnt++;}} P-=d; pos=no[i].a1; Pq.push (NO[I].B1);} if (!sign) {printf ("%d\n", CNT);}} return 0;}
Here's another way
#include <cstdio> #include <algorithm> #include <queue>using namespace std;struct node{int a1,b1;} No[10010];int CMP (const node&a, const NODE&B) {return a.a1<b.a1;} int main () {int t;priority_queue<int>pq;while (scanf ("%d", &t)!=eof) {while (!pq.empty ()) Pq.pop (); for (int i= 0;i<t;i++) {scanf ("%d%d", &NO[I].A1,&NO[I].B1);} int l,p;scanf ("%d%d", &l,&p); for (int i=0;i<t;i++) {no[i].a1=l-no[i].a1;} Sort (no,no+t,cmp), int cur=p;//current position int i=0;int Cnt=0;while (cur<l) {//If not yet to finish while (no[i].a1<=cur&&i <T) {//can refuel as far as possible add Pq.push (NO[I].B1); i++;} if (Pq.empty ()) break;cur+=pq.top (); The current position moves forward (plus the oil at the current largest petrol station) Pq.pop (); cnt++;} if (cur>=l)//When the farthest running distance exceeds the end of printf ("%d\n", CNT); elseprintf (" -1\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
poj2431 Expedition (Priority queue) Challenge Program Design Contest