Traffic light
Time limit2sMemory limit512 MB
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 it turns green for the first road and red for the second. After seconds it switches to red for the first road and green for the second. After seconds it switches back to green for the first road, and so on.
So the first road is green at times for integer , and the second road is green at times . Switches happen at and . 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 .
The technical documentation fixes the period at , so must hold. Under this condition, and 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 , there are cars on the first road. The -th car is meters from the intersection and moves toward it at m/s.
Similarly, there are cars on the second road. The -th car is meters from the intersection and moves toward it at m/s.
To reduce jams, choose and 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 and .
Input
The first line contains a real number () with at most three digits after the decimal point.
The second line contains (), the number of cars on the first road. The next lines each contain and (), the distance from the car to the intersection and its speed.
The next line contains (, ), the number of cars on the second road. The next lines each contain and (), the distance and speed of each car.
All values , , , 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 such that some choice of and keeps at most cars standing at the intersection at any time.
On the second line, print two real numbers and 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 . 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 , 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 , and both slow to 1 m/s. The three cars reach the intersection together, seconds after the start.
On the second road, the cars move at equal speed and reach the intersection , , and seconds after the start. No choice of and avoids making at least one car wait. The best choice is any from to , 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 , all three cars on the first road must wait. If , the second and third cars on the second road wait at the intersection at the same time.