luogu#P16368. [OOI 2026] March of the Sheep

[OOI 2026] March of the Sheep

Problem Description

Sasha is tired of the heavy city life and decided to move to the countryside. To occupy his time, he decided to raise sheep. For this purpose, he acquired a rectangular field in the village, which can be represented as a grid of size n×mn \times m, where the rows are numbered from top to bottom with numbers from 11 to nn, and the columns are numbered from left to right with numbers from 11 to mm.

On his field, Sasha bought nn sheep, each of which he assigned a whole row. Initially, the ii-th sheep was placed in the cell with coordinates (i,ai)(i, a_i) (in row number ii and column number aia_i). Sasha found out that the sheep move only horizontally according to the following rules:

  • If there is exactly one column in the field, the sheep do not move.
  • Initially, the ii-th sheep is in cell (i,ai)(i, a_i), and each sheep has an initial direction in which it moves --- either left or right. There is no sheep that is in the first column and moves left, nor is there any sheep that is in the last column and moves right.
  • Every second, each sheep moves one column to the left if it is moving left; otherwise, it moves one column to the right.
  • If after moving a sheep ends up in column 11 and is moving left, or if a sheep ends up in the last column and is moving right, it changes its direction to the opposite. Thus, the sheep never leave the boundaries of the field.

:::align{center}

Here is an example of the sheep's movement, where initially the first sheep was in column 11 and moved to the right, and the second was in column 33 and moved to the left. The first picture shows the state at the beginning, the second after one second, the third after 22 seconds from the start of movement, and the fourth after 33 seconds from the start of movement. :::

Sasha does not like that the sheep move so chaotically; he wants all the sheep to move uniformly. This means that all sheep should be in the same column and have the same direction of movement. To achieve this, Sasha can trim his field several (possibly zero) times as follows:

  • He chooses a time tt (how many seconds have passed since the initial start of the sheep's movement) and the number of columns xx that will remain in the field after trimming.
  • All sheep at time tt must strictly lie within the field of size n×xn \times x. This means that for any ii from 11 to nn, the ii-th sheep at time tt must be in cell (i,yi)(i, y_i), where 1yix1 \leq y_i \leq x. Otherwise, such a trimming of the field is not allowed.
  • After this operation, starting from time tt, the number of columns on the field decreases to xx.
  • Each sheep that is in the last column and moves to the right changes its direction to the opposite.

Detailed examples of the trimming process are described in the notes section.

Sasha wants to know if it is possible to achieve that all sheep move uniformly by trimming the field several times. If this is possible, Sasha wants to know what the maximum number of columns that can remain on the field is and how exactly he can achieve this. Help Sasha solve this problem!

Input Format

The first line contains one integer TT (1T31 \leq T \leq 3) --- a parameter indicating what information about the answer needs to be output. A detailed description is provided in the "Output format".

The second line contains two integers nn and mm (2n200,0002 \leq n \leq 200,000, 2m1092 \leq m \leq 10^9) --- the number of rows and columns of the field.

The third line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1aim1 \leq a_i \leq m) --- the column numbers where the sheep are initially located.

The fourth line contains a string ss of length nn, consisting only of the characters L\mathtt{L}, R\mathtt{R}, where the ii-th character is L\mathtt{L} if the ii-th sheep is initially moving left, and R\mathtt{R} if the ii-th sheep is initially moving right. It is guaranteed that no sheep in the first column is moving left, and no sheep in the mm-th column is moving right.

Output Format

In the first line, output "No" (without quotes) if Sasha cannot achieve that all sheep move uniformly. Otherwise, output "Yes" (without quotes).

If T=2T = 2 or T=3T = 3, then in the second line output the maximum number of columns that Sasha can leave on the field so that all sheep move uniformly. If T=1T=1, then this should not be output\textbf{this should not be output}.

If T=3T = 3, in the third line output the number qq (0q1060 \leq q \leq 10^6) --- how many times Sasha will need to trim the field. In the next qq lines, output the description of the trimmings.

For the description of each trimming of the field, output in one line two integers tt and xx (0t10180 \leq t \leq 10^{18}, 1x<m1 \leq x < m), where tt is the time at which Sasha needs to trim the field (from the very beginning of the sheep's movement), and xx is the number of columns that will remain on the field after this operation.

The operations must be output in non-decreasing order of tt. In each subsequent trimming, xx must be less than in the previous one.

If there are multiple suitable sequences of trimmings, any of them can be output.

It can be shown that under the given constraints, if a solution exists, then there exists a solution that fits within the given limits.

In case T=1T = 1 or T=2T = 2, do not output the description of the trimmings.

3
2 3
1 3
RL
Yes
2
1
3 2
3
2 3
1 2
RL
No
3
3 5
1 3 5
RRL
Yes
3
2
1 4
2 3
2
3 5
1 3 5
RRL
Yes
3
1
3 5
1 3 5
RRL
Yes
3
3 7
3 3 5
RRL
Yes
4
3
0 6
0 5
1 4

Hint

Note

Consider the third example:

The states of the sheep at zero seconds.

:::align{center} :::

The state of the sheep at one second.

:::align{center} :::

Trimmed the field to 44 cells.

:::align{center} :::

The state of the sheep at two seconds.

:::align{center} :::

Scoring

The tests for this problem consist of ten groups. Points for each group are awarded only if all tests of the group and all tests of some of the previous groups are passed. Note that passing the tests from the statement is not required for some groups. Offline testing\textbf{Offline testing} means that the results of testing your solution on this group will become available only after the end of the competition. The final score for each group equals the maximum score obtained for that group of tests across all submissions.

Group Points Additional constraints < Necessary groups Comment
TT nn mm
00 -- -- -- Tests from the statement.
11 88 T1T \leq 1
22 1111 T2T \leq 2 n3n \leq 3 m4m \leq 4
33 99 -- n=2n = 2 -- a1=1,a2=ma_1 = 1, a_2 = m
44 1212 m200,000m \leq 200,000
55 88 -- 3,43, 4
66 1414 T2T \leq 2 n1000n \leq 1000 m1000m \leq 1000 22
77 1010 -- -- 1,2,61, 2, 6
88 99 -- -- The string ss contains only the character R\mathtt{R}.
99 1212 n1000n \leq 1000 m1000m \leq 1000 0,2,60, 2, 6
1010 77 -- 00--99 Offline testing.