Robot Walk CodeChef SnackDown 2021 Solution
Question
Chef has recently learned robotics and has now been able to construct a robot that's much better than his robots from last year. It's still somewhat limited, but it's definitely more flexible than before. Here are its properties:
- The robot can only move forward towards the direction it faces, and can only face either horizontally or vertically.
- The robot can only turn at 90 degree angles.
- The robot is designed to turn exactly N times.
In addition, this robot is programmable. You can feed it 2N+1 values: a0, f1, a1, f2, a2, ..., fN, aN, where each ai is a positive integer and each fi is either L or R, and then it will perform the following actions:
- Initially face north.
- Move forward by a0 steps.
- If f1 is L, turn left 90 degrees; if f1 is R, turn right 90 degrees.
- Move forward by a1 steps.
- If f2 is L, turn left 90 degrees; if f2 is R, turn right 90 degrees.
- Move forward by a2 steps.
- ...
- If fN is L, turn left 90 degrees; if fN is R, turn right 90 degrees.
- Move forward by aN steps.
Now, Chef wants to test his new robot. Chef has already prepared the 2N+1 values to feed to the robot. However, Chef is very lazy and doesn't want to move, so he wants the robot to return to its starting point exactly. To do so, he can change some of the values, but some changes need time. Specifically,
- Chef can increase or decrease any ai by 1, but this takes 1 second. Also, this change is only allowed if the new ai value is positive.
- Chef can replace any fi by L or R. This takes 0 seconds.
Now, Chef wants to know the minimum number of seconds he needs to accomplish this task. Since Chef is lazy, he asks you to solve this problem for him.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N. The second line contains 2N+1 space-separated values a0, f1, a1, f2, a2, ..., fN, aN.
Output
Constraints
- 1 ≤ T ≤ 50
- 1 ≤ N, ai ≤ 500
- fi is either L or R
Example Input
2 3 11 L 7 L 11 R 7 3 11 L 9 L 11 R 7
Example Output
0 2
Explanation
Example case 1. Chef can replace f3 by L, which takes 0 seconds, so that the robot goes back to its starting position. Chef can also replace f1 and f2 by R instead, which also takes 0 + 0 = 0 seconds.
Example case 2. Chef can decrease a1 twice and then replace f3 by L, which takes 2 + 0 = 2 seconds, so that the robot goes back to its starting position. It can be shown that this is the minimum number of seconds Chef needs to accomplish this task.
Comments
Post a Comment