POJ 2431 Expedition (Adventure)
Time limit:1000ms Memory limit:65536k
"Description" |
"Title 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. |
A group of cows robbed a truck to explore the jungle. Unfortunately, not the old driver, to catch the car on the stone to die, finally folded the fuel tank June. Now every unit of travel will leak a portion of oil. In order to repair the car, the cows need to go through thousands of million resistance to the nearest town (no more than 1,000,000 units away). On the journey There are N (1 <= n <= 10,000) petrol stations available for refueling (1 per petrol station). 100 parts of the oil). Mountains, both human and animal are harmed. So the cows want to go to the gas station as little as possible on their way to town. Fortunately, the car is four times the fuel tank, the wood is loaded with oil caps. The truck now has an L unit distance from the town and has a p portion of oil (1 <= P <= 1,000,000). Find the minimum number of refueling times to reach the town, or the herd cannot reach the town. |
"Input" |
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 |
* Line 1th: an integer n * 2nd. N+1 Line: Two spaces separated by a line of integers represent a petrol station: The first integer represents the distance from the town to the petrol station, and the second represents the amount of oil in the gas station. * Line n+2: Two integers separated by a space, L and P |
"Output" |
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. |
* Line 1th: An integer representing the minimum number of gas stations needed to reach the town. If not, output-1. |
"Sample Input-Enter sample" |
"Sample output-Output sample" |
4 4 4 5 2 11 5 15 10 25 10 |
2 |
"Hint" |
"prompt" |
| INPUT DETAILS: The truck is UN Its 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: Units, stop to acquire ten more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then D Rive to the town. |
Input Description: The truck is 25 units long from the town and has 10 units of petrol. Along the journey there were 4 petrol stations off the town of 4,5,11 and 15 (so they started out from the truck 21,20,14 and 10). These petrol filling stations can provide 4,2,5 and 10 parts of petrol respectively. Output Description: Drive 10 unit lengths, get 10 parts of petrol, drive 4 unit lengths, get 5 parts of petrol, and then head to town. |
Exercises
Because the tank is infinite capacity, the oil quantity = = can advance the distance.
So we can turn the problem into a bit:
How much oil can go far, has been past the gas station can be empty refueling at any time.
As for how to choose a petrol station, each time the most greedy oil gas station on the line.
"Code C + +"
1#include <cstdio>2#include <queue>3#include <algorithm>4std::p riority_queue<int, std::vector<int> >fuel;5 structstop{6 intfuel, distant;7}data[10005];8 BOOLCMP (Stop A, stop B) {9 returnA.distant <b.distant;Ten } One intMain () { A intI, J, N, L, Nowl, opt; -scanf"%d", &n); - for(i =0; I < n; ++i) { thescanf"%d%d", &l, &nowl); -Data[i].distant =-L; Data[i].fuel =Nowl; - } -scanf"%d%d", &l, &nowl); + for(i =0; I < n; ++i) Data[i].distant + =L; -Std::sort (data, Data +N, CMP); + A for(i = opt =0; Nowl < L; ++opt) { at while(I < n && data[i].distant <= nowl) Fuel.push (data[i++].fuel); - if(Fuel.empty ()) Break; -Nowl + =fuel.top (), Fuel.pop (); - } - if(Nowl < L) puts ("-1"); - Elseprintf"%d", opt); in return 0; -}
POJ 2431 Expedition (Adventure)