#P6261. Traffic Lights Synchronization
Traffic Lights Synchronization
Traffic Lights Synchronization
Consider a straight road called Main Street with several traffic lights positioned at various locations. Each traffic light repeatedly turns red for r seconds and green for g seconds (r and g may be different for each light). At time 0, every traffic light has just turned red.
An ideal car appears magically at the west end of Main Street at a uniformly random real time in the interval \([0, 2019!]\), and it drives eastward at a constant speed of 1 meter/second. When the car reaches a traffic light located at position x (in meters), it observes the light; if the light is green it continues, but if it is red the car is forced to stop immediately and remains there forever.
Your task is to compute two probabilities:
- The probability that the car will make it through all the traffic lights without stopping.
- If the car is forced to stop, the probability that the first red light it encounters is a specific traffic light (when the lights are considered in order along Main Street from west to east).
Note: For a traffic light with parameters r and g, its cycle length is \(T = r+g\). At a time \(t\) when the car reaches a light at position \(x\), the condition for the light to be green is
\( (t+x) \bmod T \in [r, r+g) \).
Because the departure time is uniformly distributed over an interval that is a multiple of each cycle, the probability that a particular light is green when reached is \(\frac{g}{r+g}\), and similarly red is \(\frac{r}{r+g}\). Assuming the lights are processed in order of increasing position, the overall probability of never stopping is the product
[ P(\text{success}) = \prod_{i=1}^{n} \frac{g_i}{r_i+g_i}. ]
Furthermore, the probability that the car is stopped for the first time at the (i)-th light (after having seen all previous lights green) is
[ P(\text{first red at light } i) = \left(\prod_{j=1}^{i-1} \frac{g_j}{r_j+g_j}\right) \cdot \frac{r_i}{r_i+g_i}. ]
Input details follow below.
inputFormat
The input begins with a single integer n (the number of traffic lights). Each of the following n lines contains three space‐separated integers:
- x: the position (in meters) of a traffic light along Main Street,
- r: the duration (in seconds) the light is red,
- g: the duration (in seconds) the light is green.
The traffic lights can be given in any order. You should process them in order of increasing x (i.e. from west to east).
outputFormat
The output consists of two lines:
- The first line contains a floating point number representing the probability that the car makes it through all the lights without stopping. Print this probability with 6 decimal places.
- The second line contains n floating point numbers (each printed with 6 decimal places) separated by a space. The i-th number is the probability that the car is forced to stop for the first time at the i-th traffic light (when sorted in increasing order of position).
Note: The probabilities should be computed exactly as follows:
- \(P(\text{success}) = \prod_{i=1}^{n} \frac{g_i}{r_i+g_i}\).
- \(P(\text{first red at light } i) = \left(\prod_{j=1}^{i-1} \frac{g_j}{r_j+g_j}\right) \cdot \frac{r_i}{r_i+g_i}\).
sample
2
10 30 30
50 20 40
0.333333
0.500000 0.166667
</p>