Help..!!
Practice
4.9 (10 votes)
Algorithms
Greedy algorithms
Medium
Open
Priority queue
Sorting
Hiring
Problem
91% Success 7529 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A group of friends is going on a vacation to a beach by a car. One of them is suffering from a severe fever and needs to be taken to hospital in nearest town immediately.

Assume that car consumes one unit of petrol every unit of distance it travels. The hospital is located in town situated at co-ordinate 0. The car is D units away from the town. On this road, between the town and the current location of the car, there are N petrol stations where the friends can stop to acquire additional petrol.

As the fever is getting worse with time, the friends want to make the minimum possible number of stops for petrol on the way to the town. Fortunately, the capacity of the petrol tank on their car is so large that there is no limit to the amount of petrol it can hold. The car has some initial amount of petrol in tank which is denoted by P.

Determine the minimum number of stops needed to reach the town, or if the freinds cannot reach the town at all.

Note:
The town is situated at co-ordinate 0. All the other distances are given with respect to town's location.

Input Format:

  • First line contains an integer N, number of petrol stations on the way to town.
  • Each of the next N lines contains 2 space-separated integers S and F describing a petrol station: S, the distance from the town to the station and F, the amount of petrol available at that station.
  • Next line contains 2 space separated integers D and P.

Output Format:

  • Output a single integer giving the minimum number of stops necessary to reach the town. If it is not possible to reach the town, output 1.

Input Constraints:

  • \(1\le N \le 10^5 \)
  • \( 1 \le D,P,S,F \le 10^{9} \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
C++Greedy AlgorithmsAlgorithmsObservationSliding WindowBasics of Greedy Algorithms
Points:20
Tags:
Basic ProgrammingEasy
Points:30
6 votes
Tags:
Basics of Greedy AlgorithmsGreedy AlgorithmsAlgorithms