Problems > Fewest Refuels

Fewest Refuels

Difficulty: 2000 5000 ms 128 MB

greedy heap

A car starts with S fuel (1 fuel = 1 km) and must reach position TARGET. Fuel stations sit at given positions, each offering a fixed amount. Print the minimum number of stops needed, or −1 if unreachable.

Input: the first line has TARGET, S and N; each of the next N lines has a station's position and fuel.

Output: the minimum number of refuel stops, or −1.

Constraints:

  • 1 ≤ TARGET ≤ 109
  • 0 ≤ S ≤ TARGET
  • 0 ≤ N ≤ 105

Sample Tests

Input 1
100 10 4
10 60
20 30
30 30
60 40
Output 1
2
Input 2
5 5 0
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.