Green Light
Time limit1sMemory limit512 MB
Given color observations and fixed traffic-light phase lengths, find the probability that the unknown cycle offset gives color cq at time tq.
- Level
Medium7 of 10
- Topics
- Math, Intervals, Brute force, Number theory
- Solved
- No attempts yet
Problem
Sarah is cycling to work. On her way there, she encounters the same traffic light every day. Before she reaches the light, she alternates between using social media on her mobile device and glancing at the traffic light, observing whether it is green, yellow, or red at that moment. From experience, she knows that the light has a fixed green-yellow-red cycle and how long each stage lasts. So if the light goes from red to green at time T, she knows it will stay green until (but not including) T + Tg, then go yellow until (but not including) T + Tg + Ty, and finally stay red until (but not including) T + Tg + Ty + Tr, at which point it will turn green again. However, she does not know T, the time at which the traffic light cycle starts. Based on her observations, she can deduce which values of T are possible and which are impossible. Assuming that each possible value of T consistent with her observations is equally likely, can you compute the probability that the light will be green at a certain time?
Input
- The first line contains three positive integers Tg, Ty, Tr, the durations (in seconds) for which the light stays green, yellow, and red (0 < Tg, Ty, Tr ≤ 108).
- The second line contains a single positive integer n, the number of times Sarah looked at the light (3 ≤ n < 1000).
- Each of the next n lines contains one integer 0 ≤ t ≤ 109 followed by a color c: the time (in seconds) of the observation and the color of the light at that moment. Sarah did see the light being each color (green, yellow, and red) at least once.
- The last line contains an integer 0 ≤ tq ≤ 109 and a color cq. These specify the question asked: what is the probability of the light being color cq at time tq?
Output
- Output 0 ≤ p ≤ 1, the probability of the light being color cq at time tq. Your answer is correct when it differs from the actual answer by at most 10−3, either absolutely or relatively.