Right-hand obstruction
InterviewTime limit1sMemory limit512 MB
Cars arrive in four queues at a crossroad; a front car passes only if the queue on its right is empty, so simulate second by second and report each car's crossing time or -1.
- Level
Hard8 of 10
- Topics
- Simulation, Queue, Implementation, Greedy
- Solved
- No attempts yet
Problem
Every morning the residents of the capital of Berland have to stand in terrible traffic jams on their way to work. These jams are especially bad at the central square of the capital, which is an intersection and, moreover, an uncontrolled one: the people of Berland want to keep the historic look of the city center untouched.
Having decided to study the situation, the mayor of the capital ordered an investigation into exactly how the jams pile up. Turns are forbidden at the intersection, so cars can only drive straight through it. After installing special sensors, the specialists found that every morning cars try to drive through the intersection. Streets approach the intersection from four sides: if you look at a map, these streets run in the directions up <<U>>, left <<L>>, down <<D>>, and right <<R>>. On each street, a queue of cars can accumulate while they pass through the intersection.
When approaching the intersection, each driver acts as follows. The -th driver arrives at the intersection at the beginning of second , joins the end of the queue on that street, and analyzes the situation.
If at the moment of analysis there is at least one other car ahead of the driver in the queue, he keeps waiting and analyzes the situation again at the beginning of the next second. If there are no cars ahead of him in the queue, he tries to drive through the intersection. If the driver has no right-hand obstruction, he leaves the queue, drives through the intersection during that second, and gets out of the trouble spot. Otherwise he stays in the queue and analyzes the situation again at the beginning of the next second. Drivers analyze the situation simultaneously, and only after that can the first driver in the queue start moving, so at most one car per second can pass through the intersection from each direction.
A driver at the intersection has a right-hand obstruction if there is at least one car in the queue on the perpendicular direction to his right. So drivers trying to pass the intersection in the up direction are obstructed by cars standing in the queue in the left direction, the left direction is obstructed by cars from the queue in the down direction, and so on. Note that if all four queues are non-empty, then every driver has a right-hand obstruction, and they will never pass the intersection.
Given the times at which the drivers arrived at the intersection, find out when each of them passes the intersection. Note that some drivers may never pass the intersection, remaining in the queue in front of it.
Input
The first line contains an integer , the number of cars approaching the intersection ().
Each of the next lines contains a number and a character : the number of the second at the beginning of which the -th car arrives at the intersection, and the direction in which it tries to pass it (; is <<U>> if the car goes up on the map, <<L>> if it goes left, <<D>> if down, and <<R>> if right). The cars are given in nondecreasing order of .
It is guaranteed that at any moment at most one new car approaches from each side.
Output
For each car, in the order it is described in the input, output on a separate line the number of the second when it passes the intersection. If a car remains standing at the intersection, output on the corresponding line.