#abc376f. F - Hands on Ring (Hard)

F - Hands on Ring (Hard)

Score : 550550 points

问题陈述

注意:这个问题的设置几乎与问题B相同。只有在正文中加粗的部分和约束条件有所不同。

你用双手拿着一个环。这个环由N (N3)N\ (N \geq 3)部分组成,编号为1,2,,N1,2,\dots,N,其中第ii部分和第i+1i+1部分(1iN11 \leq i \leq N-1)是相邻的,第1部分和第NN部分也是相邻的。

最初,你的左手拿着第1部分,右手拿着第2部分。在一个_操作_中,你可以做以下事情:

  • 将一只手移动到它当前持有的部分的相邻部分。但是,只有当另一只手不在目的地部分时,你才能这样做。

下面的图示显示了初始状态以及可以从那里进行和不能进行的操作示例。环的每个部分上写的数字代表部分编号,标有L和R的圆圈分别代表你的左手和右手。

你需要按照顺序遵循给你的QQ个指令。第ii个(1iQ1 \leq i \leq Q)指令由一个字符HiH_i和一个整数TiT_i表示,含义如下:

  • 执行一些操作(可能是零),以便你的左手(如果HiH_iL)或右手(如果HiH_iR)拿着第TiT_i部分。在这里,你可以移动HiH_i未指定的另一只手。

在这个问题的设置和约束条件下,可以证明任何指令都是可实现的。

找出遵循所有指令所需的最小总操作数。

以上为大语言模型 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 N (N3)N\ (N \geq 3) parts numbered 1,2,,N1,2,\dots,N, where parts ii and i+1i+1 (1iN11 \leq i \leq N-1) are adjacent, and parts 11 and NN are also adjacent.

Initially, your left hand is holding part 11, and your right hand is holding part 22. 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 QQ instructions given to you in order. The ii-th (1iQ1 \leq i \leq Q) instruction is represented by a character HiH_i and an integer TiT_i, meaning the following:

  • Perform some number of operations (possibly zero) so that your left hand (if HiH_i is L) or your right hand (if HiH_i is R) is holding part TiT_i. Here, you may move the other hand not specified by HiH_i.

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

  • 3N30003\leq N \leq 3000
  • 1Q30001\leq Q \leq 3000
  • HiH_i is L or R.
  • 1TiN1 \leq T_i \leq N
  • NN, QQ, and TiT_i are integers.

Input

The Input is given from Standard Input in the following format:

NN QQ

H1H_1 T1T_1

H2H_2 T2T_2

\vdots

HQH_Q TQT_Q

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 QQ instructions in order.

  1. Move your right hand as part 2342 \rightarrow 3 \rightarrow 4 to follow the first instruction.
  2. Move your left hand as part 1651 \rightarrow 6 \rightarrow 5 to follow the second instruction.
  3. Move your left hand as part 565 \rightarrow 6, then move your right hand as part 454 \rightarrow 5 to follow the third instruction.

In this case, the total number of operations is 2+2+1+1=62+2+1+1=6, 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