#abc376b. B - Hands on Ring (Easy)
B - Hands on Ring (Easy)
Score : points
问题陈述
注意:这个问题的设置几乎与问题F相同。只有在正文中加粗的部分和约束条件有所不同。
你用双手拿着一个环。这个环由部分组成,编号为,其中第部分和第部分()是相邻的,第1部分和第N部分也是相邻的。
最初,你的左手拿着第1部分,右手拿着第2部分。在一个 操作 中,你可以做以下事情:
- 将一只手移动到它当前持有的部分的相邻部分。但是,只有当另一只手不在目的地部分时,你才能这样做。
下面的图表显示了初始状态以及可以从那里进行和不能进行的操作示例。环的每个部分上写的数字代表部分编号,标记为L和R的圆圈分别代表你的左手和右手。
你需要按照顺序遵循给你的个指令。第个()指令由一个字符和一个整数表示,含义如下:
- 执行一些操作(可能是零),使你的左手(如果是
L
)或右手(如果是R
)拿着第部分。这里,你不能移动未指定的另一只手。
保证只给出可实现的指令。
在这个问题的设置下,可以证明,在遵循第个指令之前,两只手的位置是唯一确定的。在那时,如果我们将左手和右手的位置分别表示为部分和,那么当是L
时,保证,当是R
时,保证。
找出遵循所有指令所需的最小总操作数。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
Note: This problem has almost the same setting as Problem F. Only the parts in bold in the main text and constraints differ.
You are holding a ring with both hands. This ring consists of parts numbered , where parts and () are adjacent, and parts and are also adjacent.
Initially, your left hand is holding part , and your right hand is holding part . In one operation, you can do the following:
- Move one of your hands to an adjacent part of the part it is currently holding. However, you can do this only if the other hand is not on the destination part.
The following figure shows the initial state and examples of operations that can and cannot be made from there. The number written on each part of the ring represents the part number, and the circles labeled L and R represent your left and right hands, respectively.
You need to follow instructions given to you in order. The -th () instruction is represented by a character and an integer , meaning the following:
- Perform some number of operations (possibly zero) so that your left hand (if is
L
) or your right hand (if isR
) is holding part . Here, you must not move the other hand not specified by .
It is guaranteed that only achievable instructions are given.
Details Under the settings of this problem, it can be proved that the positions of both hands are uniquely determined just before following the -th instruction for each . At that time, if we denote the positions of the left and right hands as parts and , respectively, it is guaranteed that when is L
, and when is R
.
Find the minimum total number of operations required to follow all the instructions.
Constraints
- is
L
orR
. - , , and are integers.
- Only achievable instructions are given (see the problem statement for details).
Input
The Input is given from Standard Input in the following format:
Output
Print the minimum total number of operations required to follow all the instructions.
Sample Input 1
6 3
R 4
L 5
R 6
Sample Output 1
8
By performing the following operations, you can follow all instructions in order.
- Move your right hand as part to follow the first instruction.
- Move your left hand as part to follow the second instruction.
- Move your right hand as part $4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 6$ to follow the third instruction.
In this case, the total number of operations is , which is the minimum. (Note that when following the third instruction, you cannot move your right hand as part .)
Sample Input 2
100 2
L 1
R 2
Sample Output 2
0
There are cases where you can follow the instructions without performing any operations.
Sample Input 3
30 8
R 23
R 26
R 29
L 20
R 29
R 19
L 7
L 16
Sample Output 3
92