Button-Pressing Robots
InterviewTime limit5sMemory limit512 MB
Two robots walk through separate hallways in parallel and press one shared ordered list of buttons in the shortest total time.
- Level
Easy3 of 10
- Topics
- Simulation, Greedy
- Solved
- No attempts yet
Problem
The blue robot and the orange robot are locked in two separate hallways. Each hallway holds 100 buttons numbered 1 to 100, and button sits meters from the hallway entrance. Both robots start in front of button 1.
During one second a robot does exactly one of these three things.
- Walk one meter in either direction.
- Press the button at its current position once.
- Stay in place and do nothing.
The robots must press a given list of buttons in the given order. Both robots know the whole list in advance, and both act during every second. A button cannot be pressed until the press of the previous button in the list has finished. Compute the minimum number of seconds needed to press every button in the list, in order.
O 2 means button 2 in the orange robot's hallway, and B 1 means button 1 in the blue robot's hallway. For instance, the order O 2, B 1, B 2, O 4 can be finished in 6 seconds with the plan below.
Blue can press B 1 only after orange has finished pressing O 2.
Input
The first line contains the number of test cases .
Each of the next lines holds one test case. The line starts with the number of buttons to press, , followed by items in the order and so on. is the robot color, either O or B, and is a button number. All values are separated by spaces.
Limits
- for every
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the minimum number of seconds needed to press the given buttons in order.