#P13361. [GCJ 2011 Qualification] Bot Trust

[GCJ 2011 Qualification] Bot Trust

题目描述

Blue and Orange are friendly robots. An evil computer mastermind has locked them up in separate hallways to test them, and then possibly give them cake.

Each hallway contains 100100 buttons labeled with the positive integers {1,2,,100}\{1, 2, \ldots, 100\}. Button kk is always kk meters from the start of the hallway, and the robots both begin at button 11. Over the period of one second, a robot can walk one meter in either direction, or it can press the button at its position once, or it can stay at its position and not press the button. To complete the test, the robots need to push a certain sequence of buttons in a certain order. Both robots know the full sequence in advance. How fast can they complete it?

For example, let's consider the following button sequence:

O 22, B 11, B 22, O 44

Here, O 22 means button 22 in Orange's hallway, B 11 means button 11 in Blue's hallway, and so on. The robots can push this sequence of buttons in 66 seconds using the strategy shown below:

Time Orange Blue
11 Move to button 22 Stay at button 11
22 Push button 22
33 Move to button 33 Push button 11
44 Move to button 44 Move to button 22
55 Stay at button 44 Push button 22
66 Push button 44 Stay at button 22

Note that Blue has to wait until Orange has completely finished pushing O 22 before it can start pushing B 11.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case consists of a single line beginning with a positive integer NN, representing the number of buttons that need to be pressed. This is followed by NN terms of the form "RiPiR_i P_i" where RiR_i is a robot color (always 'O' or 'B'), and PiP_i is a button position.

输出格式

For each test case, output one line containing "Case #x: y", where xx is the case number (starting from 1) and yy is the minimum number of seconds required for the robots to push the given buttons, in order.

3
4 O 2 B 1 B 2 O 4
3 O 5 O 8 B 100
2 B 2 B 1
Case #1: 6
Case #2: 100
Case #3: 4

提示

Limits

  • 1Pi1001 \leq P_i \leq 100 for all ii.

Small dataset (10 Pts, Test set 1 - Visible)

  • 1T201 \leq T \leq 20.
  • 1N101 \leq N \leq 10.
  • Time limit: 30 3 seconds.

Large dataset (10 Pts, Test set 2 - Hidden)

  • 1T1001 \leq T \leq 100.
  • 1N1001 \leq N \leq 100.
  • Time limit: 60 6 seconds.