Bicycle

Time limit1sMemory limit128 MB

Summary
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 0.5 m/s20.5\,\mathrm{m/s^2} (its speed can increase by at most 0.5 m/s0.5\,\mathrm{m/s} each second).
  • The bicycle can instantly decelerate to any speed between 00 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 00) at position X=0X = 0 at time T=0T = 0 and wants to reach the destination X=XdestX = X_{dest} as quickly as possible. Passing every traffic light only while it is green, find the earliest possible time to arrive at XdestX_{dest}.

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 XdestX_{dest} and the number of traffic lights LL (1≤Xdest≤100001 \le X_{dest} \le 10000, 0≤L≤100 \le L \le 10).

The following LL lines describe the traffic lights in order of increasing XX coordinate. Each line contains the light's position XiX_i (0<Xi<Xdest0 < X_i < X_{dest}), the duration RiR_i for which it stays red (10≤Ri≤50010 \le R_i \le 500), and the duration GiG_i for which it stays green (10≤Gi≤50010 \le G_i \le 500).

Every traffic light starts red at T=0T = 0; light ii turns green for the first time at T=RiT = R_i. It then stays green for GiG_i 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.

Examples4

  1. Example 1

    Input
    410.0 2
    200.0 15.0 15.0
    225.0 31.0 10.0
    410.0 2
    200.0 15.0 15.0
    225.0 35.1 15.0
    410.0 2
    200.0 15.0 15.0
    225.0 45.0 10.0
    
    Expected output
    41.497
    52.623
    57.213
    
  2. Example 2

    Input
    100.0 0
    
    Expected output
    20.000
    
  3. Example 3

    Input
    500.0 1
    100.0 25.0 10.0
    
    Expected output
    49.721
    
  4. Example 4

    Input
    250.0 0
    250.0 1
    90.0 22.0 11.0
    700.0 2
    160.0 30.0 15.0
    360.0 50.0 20.0
    
    Expected output
    31.623
    34.649
    64.968