google code jam 2011 Qualification Round 资格赛 Problem A. Bot Trust

Problem

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 100 buttons labeled with the positive integers {1, 2, ..., 100}. Button k is always k meters from the start of the hallway, and the robots both begin at button 1. 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 2, B 1, B 2, O 4

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

Time | Orange           | Blue
-----+------------------+-----------------
  1  | Move to button 2 | Stay at button 1
  2  | Push button 2    | Stay at button 1
  3  | Move to button 3 | Push button 1
  4  | Move to button 4 | Move to button 2
  5  | Stay at button 4 | Push button 2
  6  | Push button 4    | Stay at button 2

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

 

Input

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

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

Output

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

Limits

1 ≤ Pi ≤ 100 for all i.

Small dataset

1 ≤ T ≤ 20.
1 ≤ N ≤ 10.

Large dataset

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.

Sample


Input
 

Output
 

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

solution :
本题可用动态规划的思想,当前所需的步数与上一步的步数以及和同颜色的上一步的步数有关,
举个例子 O 2 B 3 B 4 O 6
那么到O 6 push的时候的总步数与 B 4 push的总步数 和 O 2push的总步数相关,分析可以得到以下结论
currT = max(lastT,abs(currP —lastSameP) + lastSameP ) + 1;
这个式子的含义为 当前读取的为O 6 ,设O 6被按下是所需的步数为上式中的currT,lastT则为 O 6之前的数据被按下的步数,此例子中为B 4 被按下的步数,而currp则为当前按下的数字,此时为O 6中的6,lastSameP 为前一个同颜色的数字,就是O 2中的2,那么
abs(currp - lastSamep)为上一个同颜色的地方走到当前位置所需要的步数,而lastSameP则为上一个同颜色的地方所需要的总步数,
所以abs(currP —lastSameP) + lastSameP ) 的含义应该也清楚了吧,最后一个1就是按下数字的一步!
所以判断的方法将简单了就是判断上一个位置的步数与上一个相同颜色位置到现在位子所需要的步数+上一个同颜色位置所需要的总步数中取较大的一项,然后加上按下数字的一步,即为当前所需的总步数。
下面给上代码 此代码参考了SkidanovAlexander的代码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值