Speed Ups
InterviewTime limit2sMemory limit1024 MB
Given speed-up boosts at fixed meter marks, find the fastest finishing time when a taken boost blocks any other boost until it ends.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
In a video game called Speed Ups, you are a single player running a race. The goal is to finish the race as fast as possible. Your regular speed is 1 meter/second. At different meter markers, you may get the choice of a speed up. Each speed up is an ordered triplet of integers . You can use it exactly meters after the start of the race, which gives you a temporary speed of meters per second for seconds. While you are using a speed up, you cannot use another one. So if you take one speed up, you may run past the location of another speed up that would have been better.
For example, suppose a 100 meter race has two speed ups: and . You run the first 10 meters in 10 seconds. If you take the first speed up, you run 5 seconds at 2 meters per second and reach the 20 meter mark at 15 seconds. That is past the second speed up at 15 meters, so you cannot take it. You then finish in seconds. If you skip the first speed up and take the second one at 15 meters, you run 20 seconds at 3 meters per second, reach the 75 meter mark at 35 seconds, and finish in 60 seconds. That is the fastest finish for this example.
Input
The first line has two integers: (), the number of speed ups, and (), the length of the race in meters.
Each of the next lines has three integers (), (), and (). This describes a speed up at meters from the start that sets your speed to meters per second for seconds.
Notes:
- Several speed ups can be at the same location, but you can take only one of them.
- Once you take a speed up, you must use it until it completes or the race ends, whichever comes first.
- You cannot use another speed up until the current one is fully completed.
- If a speed up puts you at distance from the start and a new speed up is exactly at , you can take the new one.
Output
Print the fastest possible finishing time in seconds on a single line. An answer is accepted if its absolute or relative error is within .
Hint
In the second sample, taking the first speed up is better than taking the third one, because the third one prevents you from taking the second. Taking the first speed up at 25 meters gives 25 seconds at 3 meters per second, so you reach the 100 meter mark at 50 seconds. Then you take the second speed up and run 400 seconds at 2 meters per second, reaching the 900 meter mark at 450 seconds. The last 100 meters take 100 seconds at 1 meter per second.
In the last sample, the speed up taken at 7 seconds lasts 200 seconds, but the race ends first. You run the remaining 43 meters at 4 meters per second in 10.75 seconds.