#abc376f. F - Hands on Ring (Hard)
F - Hands on Ring (Hard)
Score : points
问题陈述
注意:这个问题的设置几乎与问题B相同。只有在正文中加粗的部分和约束条件有所不同。
你用双手拿着一个环。这个环由部分组成,编号为,其中第部分和第部分()是相邻的,第1部分和第部分也是相邻的。
最初,你的左手拿着第1部分,右手拿着第2部分。在一个_操作_中,你可以做以下事情:
- 将一只手移动到它当前持有的部分的相邻部分。但是,只有当另一只手不在目的地部分时,你才能这样做。
下面的图示显示了初始状态以及可以从那里进行和不能进行的操作示例。环的每个部分上写的数字代表部分编号,标有L和R的圆圈分别代表你的左手和右手。
你需要按照顺序遵循给你的个指令。第个()指令由一个字符和一个整数表示,含义如下:
- 执行一些操作(可能是零),以便你的左手(如果是
L
)或右手(如果是R
)拿着第部分。在这里,你可以移动未指定的另一只手。
在这个问题的设置和约束条件下,可以证明任何指令都是可实现的。
找出遵循所有指令所需的最小总操作数。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
Note: This problem has almost the same setting as Problem B. 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 may move the other hand not specified by .
Under the settings and constraints of this problem, it can be proved that any instructions are achievable.
Find the minimum total number of operations required to follow all the instructions.
Constraints
- is
L
orR
. - , , and are integers.
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 5
Sample Output 1
6
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 left hand as part , then move your right hand as part to follow the third instruction.
In this case, the total number of operations is , which is the minimum.
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
58