luogu#P10608. 双人游戏

    ID: 10297 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>博弈论洛谷原创O2优化洛谷月赛

双人游戏

Background

After finishing her paper, Renko finally realizes her mistake of burying herself in work and ignoring Merry for so long. She decides to invite Merry out and devises an interesting two-player game.

Problem Description

Renko has designed a two-player game with two players, Little R and Little M, following these rules:

There is a 1×n1 \times n chessboard. Initially, the board has some black pieces, white pieces, and mm empty cells. The initial state of the board is described by a string ss of length nn, where:

  • B indicates a black piece,
  • W indicates a white piece,
  • _ indicates an empty cell.
    Note: The board may have no empty cells.

Before the game starts, both players receive an operation sequence $O = [\langle c_1, x_1 \rangle, \langle c_2, x_2 \rangle, \ldots, \langle c_m, x_m \rangle]$, where each pair ci,xi\langle c_i, x_i \rangle satisfies:

  • ci{R,M}c_i \in \{\mathtt{R}, \mathtt{M}\},
  • xix_i is an empty cell at step ii.

This sequence is public, meaning both players know who will place a piece and where at each step.

During the game, players follow the operation sequence. In the ii-th step, player cic_i places a piece (black or white, chosen by the player) at position xix_i. By the end of the game, every cell will have exactly one piece.

Little R aims to maximize the number of maximal consecutive monochromatic segments^*, while Little M aims to minimize it. Determine the final number of segments when both play optimally. It can be proven that the answer is unique.

Definition: A maximal consecutive monochromatic segment is a pair (l,r)(l, r) where lrl \leq r, such that:

  • All pieces from the ll-th to rr-th position are the same color.
  • The (l1)(l-1)-th piece (if exists) and the (r+1)(r+1)-th piece (if exists) are of a different color.

Input Format

  • The first line contains two integers nn and mm, where mm is the number of empty cells.
  • The second line contains a string ss of length nn, describing the initial board state.
  • The next mm lines each contain a character cic_i and an integer xix_i, describing the player and the position for the ii-th step.

Output Format

Output one integer: the number of maximal consecutive monochromatic segments when both players act optimally.

3 2
B__
R 3
M 2
2

3 3
___
R 1 
R 3
M 2
2
5 2
BW__B
R 4
R 3
5

Hint

Explanation

Sample #1

The final board is BWW, which is optimal for both players. The number of segments is 22.

Sample #2

Little R (controlling the first two steps) will place different colors to maximize segments. Little M then places any color. One possible final state is BWW with 22 segments.
This sample satisfies Special Property B.

Sample #3

The final board is BWBWB with 55 segments.
This sample satisfies Special Property A.

Constraints

Bundled testing is used.

$$\def\arraystretch{1.5} \begin{array}{|c|c|c|c|c|}\hline \textbf{Subtask} & \textbf{Points} & \bm{n,m \leq} & \textbf{Special Property} & \textbf{Subtask Dependencies} \cr\hline 1 & 10 & 20 & - & - \cr\hline 2 & 10 & 2 \times 10^5 & \mathbf{A} & - \cr\hline 3 & 20 & 2 \times 10^5 & \mathbf{B} & - \cr\hline 4 & 20 & 10^3 & - & 1 \cr\hline 5 & 40 & 2 \times 10^5 & - & 1,2,3,4 \cr\hline \end{array}$$

Special Properties:

  • A: All cic_i are either R or M.
  • B: All characters in ss are _.

For all data: 0m2×1050 \leq m \leq 2 \times 10^5, 1n2×1051 \leq n \leq 2 \times 10^5, mnm \leq n, and si{B,W,_}s_i \in \{\mathtt{B}, \mathtt{W}, \mathtt{\_}\}.


Translated by DeepSeek R1