Reversible Lane
Time limit1sMemory limit128 MB
Simulate traffic queues on a reversible bridge lane over all possible switch times to find the switch time minimizing total waiting cars.
- Level
Medium5 of 10
- Topics
- Simulation, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
A new bridge crosses a river between two busy communities. The bridge is the bottleneck: it has lanes in total, shared by both directions, while the approach roads on each side are wider.
Traffic is not symmetric. In the morning most cars travel from the left bank to the right bank; in the evening most travel from the right bank to the left bank. The bridge is therefore configured with lanes permanently reserved for left-to-right traffic, lanes permanently reserved for right-to-left traffic, and one central reversible lane, so that . In the morning the central lane is open for left-to-right traffic; at some moment it is switched to right-to-left traffic. Your task is to choose the best moment to switch it.
The day is split into equal time intervals, chosen so that a single lane lets exactly one car begin crossing per interval. Intervals are numbered from in the morning to in the evening. For each interval you are given how many cars arrive at the bridge on the left bank and on the right bank.
During each interval, and in each direction, three things happen in this order:
- New cars arrive at the bridge.
- Cars begin crossing in their direction, as many as there are lanes currently open for that direction (one car per open lane).
- Cars that cannot start crossing wait in the queue for the next interval.
No new cars arrive after interval . If cars are still waiting after interval , further intervals are simulated in the same way (with no arrivals) until every car has started crossing.
Reversing the central lane is not instantaneous: it must be cleared before traffic can use it in the other direction, so it stays closed for intervals. If the switch is triggered at interval (with ), then the open lanes are:
- before interval : lanes left-to-right and lanes right-to-left;
- from interval through interval (inclusive): lanes left-to-right and lanes right-to-left (the central lane is closed);
- from interval onward: lanes left-to-right and lanes right-to-left.
The total waiting time is the sum, over every interval and both directions, of the number of cars still waiting in the queue at the end of the interval (the count from step 3). Choose the interval (with ) that minimizes the total waiting time. If several intervals achieve the minimum, report the earliest one.
Input
The first line contains four integers , , , and , where , , and .
Each of the following lines describes one interval, in order from to , with two integers: the number of cars arriving on the left bank and the number arriving on the right bank during that interval. At most cars arrive on each bank during each interval.
Output
Output a single integer — the earliest interval at which switching the central lane minimizes the total waiting time.
Explanation
The table below traces the model on the first example, using the optimal switch interval . For each interval it lists the lanes open in each direction, the cars that arrive (step 1), the cars that begin crossing (step 2), and the cars left waiting (step 3). The total waiting time is intervals — for left-to-right cars and for right-to-left cars. Interval is shown to make clear that nothing is waiting from that point on.