#C5700. Minimum Stops to Destination
Minimum Stops to Destination
Minimum Stops to Destination
You are given a road with several gas stations. Each gas station is located at a certain distance from the starting point and offers a specified amount of fuel. You start your journey with an initial fuel amount F and need to reach a destination located at a distance D from the start.
At each gas station, you can refuel to extend your journey. However, you must decide when and where to stop to minimize the total number of stops. When you run out of fuel before reaching the next gas station, you can go back and refuel from the station that provides the maximum available fuel among those you have passed.
If you can reach the destination without any stops, output 0. If it's impossible to reach the destination, output -1.
The optimal strategy to solve this problem involves a greedy approach with a max-heap. Formally, suppose the current position is p with fuel f. Then, while you do not have enough fuel to reach the next station (or the destination), you refuel by selecting the gas station with the maximum fuel available from those passed. The goal is to minimize the number of stops required.
Mathematically, the refueling condition can be expressed as:
\[ \text{while } finputFormat
The input is provided via stdin in the following format:
- The first line contains an integer N, which represents the number of gas stations.
- The second line contains N space-separated integers representing the distances of the gas stations from the starting point.
- The third line contains N space-separated integers representing the amount of fuel available at each gas station.
- The fourth line contains two space-separated integers D and F, where D is the destination distance and F is the initial fuel amount.
outputFormat
Output a single integer to stdout representing the minimum number of stops required to reach the destination. If it is impossible to reach the destination, output -1.
## sample4
10 20 30 40
10 20 5 30
50 20
2