Hiring process8 levels
  1. 1 IQ Test
  2. 2 Additional Online Test (If Required)
  3. 3 Office Interview with Hiring Manager
  4. 4 Unpaid Test Task (Motivation Check)
  5. 5 Paid Test Task and Follow-Up Interview
  6. 6 Iterative Paid Test Tasks
  7. 7 Office Verification
  8. 8 Final Interview with the Founder

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 Statistics

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.
Ctrl+Enter submit · Ctrl+Space autocomplete · F11 fullscreen