Bicycle
Time limit1sMemory limit128 MB
Given a max-acceleration bicycle model with several periodic red-green traffic lights along a line, compute the earliest arrival time at a fixed destination.
- Level
Hard8 of 10
- Topics
- Simulation, Greedy, Math
- Solved
- No attempts yet
Problem
When riding a bicycle in a city, the time spent waiting at traffic lights makes up a large part of the total travel time. To get somewhere faster, this time must be reduced.
The time wasted because of traffic lights is more than just the time spent waiting at a red light: after the light turns green, it also takes time to accelerate the bicycle back up to speed.
The motion of the bicycle is modelled as follows.
- The bicycle can move forward or stay in place; it can never move backward. There is no maximum speed.
- The bicycle can accelerate by at most (its speed can increase by at most each second).
- The bicycle can instantly decelerate to any speed between and its current speed.
- The bicycle cannot pass a traffic light while it is red, i.e. it cannot move forward at that position during a red light.
- Each traffic light alternates between red and green with a fixed period. (There is no yellow light.)
This is a theoretical model and differs from real-world behaviour.
A rider starts at rest (speed ) at position at time and wants to reach the destination as quickly as possible. Passing every traffic light only while it is green, find the earliest possible time to arrive at .
Input
The input consists of several test cases; process test cases until end of file.
The first line of each test case contains the destination coordinate and the number of traffic lights (, ).
The following lines describe the traffic lights in order of increasing coordinate. Each line contains the light's position (), the duration for which it stays red (), and the duration for which it stays green ().
Every traffic light starts red at ; light turns green for the first time at . It then stays green for seconds, turns red again, and this cycle repeats.
No two traffic lights share the same position. All values may be real numbers.
Output
For each test case, print on its own line the earliest time at which the destination can be reached, rounded to three decimal places.