Under construction

Still being built. Things may change

Traffic light

Time limit2sMemory limit512 MB

Summary
Given two one-way roads with cars at known distances and speeds, choose a green and red split of a fixed period x that keeps the peak number of waiting cars as low as possible.
Level

Hard8 of 10

Topics
Binary search, Sorting, Simulation
Solved
No attempts yet

Statement

Everyone knows that cities have a big problem: traffic jams. A jam forms when too many cars try to drive down the same road at once. The main cause is intersections, and the biggest jams form right there.

In one city, a traffic light is to be installed at an intersection where two one-way roads cross.

At every moment the light is either green for cars on the first road, green for cars on the second road, or switching between these modes. Under the traffic rules, cars may drive on either road at the moment the light switches.

The light works as follows. At time 00 it turns green for the first road and red for the second. After gg seconds it switches to red for the first road and green for the second. After rr seconds it switches back to green for the first road, and so on.

So the first road is green at times (k(r+g),k(r+g)+g)\left(k(r+g), k(r+g)+g\right) for integer kk, and the second road is green at times (k(r+g)+g,(k+1)(r+g))\left(k(r+g)+g, (k+1)(r+g)\right). Switches happen at k(r+g)k(r+g) and k(r+g)+gk(r+g)+g. While a road is green, cars on it may pass, and cars on the other road may not. At a switching moment, cars from both roads may pass. A car counts as passing at a switching moment if its passing time and the switching time differ by at most 10−510^{-5}.

The technical documentation fixes the period at xx, so r+g=xr+g=x must hold. Under this condition, rr and gg may be any non-negative real numbers.

To choose good values, traffic on both roads was studied, and the following data was collected.

Several cars drive toward the intersection on each road. Cars do not overtake each other. When a car catches up to a slower one, it slows down and follows at that car's speed. Car sizes and the time needed to change speed are negligible. When a car reaches the intersection, if the light is green or switching, it passes immediately. Otherwise it stops and waits for green.

At time 00, there are nn cars on the first road. The ii-th car is aia_i meters from the intersection and moves toward it at viv_i m/s.

Similarly, there are mm cars on the second road. The ii-th car is bib_i meters from the intersection and moves toward it at wiw_i m/s.

To reduce jams, choose gg and rr so that the maximum number of cars standing at the intersection at the same time is as small as possible.

Help the traffic office choose the optimal gg and rr.

Input

The first line contains a real number xx (1≤x≤1041 \le x \le 10^4) with at most three digits after the decimal point.

The second line contains nn (0≤n≤1050 \le n \le 10^5), the number of cars on the first road. The next nn lines each contain aia_i and viv_i (1≤ai,vi≤1041 \le a_i, v_i \le 10^4), the distance from the car to the intersection and its speed.

The next line contains mm (0≤m≤1050 \le m \le 10^5, 1≤n+m≤1051 \le n+m \le 10^5), the number of cars on the second road. The next mm lines each contain bib_i and wiw_i (1≤bi,wi≤1041 \le b_i, w_i \le 10^4), the distance and speed of each car.

All values aia_i, viv_i, bib_i, wiw_i have at most three digits after the decimal point. No two cars start at the same point. Both lists are given in increasing order of distance to the light.

Output

On the first line, print the minimum kk such that some choice of gg and rr keeps at most kk cars standing at the intersection at any time.

On the second line, print two real numbers gg and rr that achieve this. Print as many digits after the decimal point as you can. If several optimal answers exist, print any of them.

Hints

In the first example, all cars reach the intersection at time 11. If the light switches exactly at that moment, every car passes without waiting.

In the second example, the first road develops as follows. At time 1/151/15, the third car catches up to the second car and slows to 5 m/s. Then both of them catch up to the first car at time 0.50.5, and both slow to 1 m/s. The three cars reach the intersection together, 22 seconds after the start.

On the second road, the cars move at equal speed and reach the intersection 11, 55, and 77 seconds after the start. No choice of gg and rr avoids making at least one car wait. The best choice is any gg from 22 to 33, inclusive. Then the first and second cars on the second road wait, but at most one car is at the intersection at any time. If g<2g < 2, all three cars on the first road must wait. If g>3g > 3, the second and third cars on the second road wait at the intersection at the same time.

Examples2

  1. Example 1

    Input
    2.0
    1
    1.0 1.0
    2
    1.0 1.0
    2.0 2.0
    
    Expected output
    0
    1.00000 1.00000
    
  2. Example 2

    Input
    4.0
    3
    2.0 1.0
    4.0 5.0
    5.0 20.0
    3
    1.0 1.0
    5.0 1.0
    7.0 1.0
    
    Expected output
    1
    2.00000 2.00000