#P10684. [COTS 2024] 分割 Segregacija
[COTS 2024] 分割 Segregacija
Background
Translated from Izborne Pripreme 2024 (Croatian IOI/CEOI Team Selection) D2T2。。
Please do not abuse the judge for this problem. If you abuse the judge for this problem, your account will be banned.
Problem Description
Pero has a matrix with rows and columns. Each cell contains either a red ball or a blue ball.
Pero wants to rearrange the balls in the matrix so that all blue balls are on the upper-left side of the matrix, and all red balls are on the lower-right side. More specifically, after rearranging, there must not exist a red ball that is above or to the left of some blue ball.
To do this, Pero can swap two adjacent balls multiple times. Two balls are adjacent if and only if the cells they are in share an edge. Pero wants to know the minimum number of swaps needed to reach the goal.
In addition, Pero will perform swaps of adjacent balls in the matrix, and after each change, he asks for the minimum number of swaps needed for the current matrix state. Please help Pero output the minimum number of swaps needed for the initial matrix, and after each swap.
Input Format
The first line contains two integers , as described in the statement.
The next two lines each contain a string of length , describing the matrix. represents a red ball, and represents a blue ball.
The next lines each contain three positive integers , describing one swap operation.
- When , swap and .
- When , swap and .
It is guaranteed that the two swapped positions are both inside the matrix.
Output Format
Output lines, representing the answer for the initial matrix and the answer after each modification.
5 2
CPCPC
PCCPC
1 1 4
1 1 2
3
4
5
5 0
CPPCC
PPCCP
4
10 7
CCPPPCCPCP
PPPCCCPCCC
1 2 7
2 1 4
2 1 8
1 1 9
2 1 1
1 2 7
1 1 4
8
9
10
10
9
8
7
8
Hint
Sample Explanation
Sample explanation: For the initial state, it is enough to swap and , then swap and , then swap and .
Constraints
For of the data, it is guaranteed that and .
| Subtask ID | Points | Constraints |
|---|---|---|
| There are at most | ||
| No additional constraints |
Translated by ChatGPT 5