#P11218. 【MX-S4-T2】「yyOI R2」youyou 不喜欢夏天
【MX-S4-T2】「yyOI R2」youyou 不喜欢夏天
Background
Original problem link: https://oier.team/problems/S4B。
Problem Description
youyou has a grid, where each cell may be black or white.
Now youyou and yy are going to play a game on this grid:
- First, youyou chooses a connected component, which is allowed to be empty.
- Then yy may choose at most columns in the grid, and swap the colors of the upper and lower cells in each chosen column.
A cell set is called a connected component if and only if any two cells in can be connected through some cells in that are edge-adjacent (i.e. 4-connected).
youyou wants to maximize the final value of (the number of black cells minus the number of white cells) inside the connected component, while yy wants to minimize it.
You need to compute: when both players use optimal strategies, what is the final value of (black cells minus white cells) in the connected component?
Input Format
This problem has multiple test cases.
The first line contains two integers , representing the subtask ID and the number of test cases. If , this subtask is the sample.
For each test case, the first line contains two integers , representing the number of columns in the grid and the number of swap operations.
The next two lines each contain a length- binary string of and , describing the colors of the grid. Here, means a black cell, and means a white cell.
Output Format
For each test case, output one line with one number, meaning the final value of (black cells minus white cells).
0 2
5 2
11110
01001
7 1
1110000
0001111
2
4
Hint
[Sample Explanation #1]
In the following, denotes the cell in row and column .
For the first test case, youyou chooses the two cells and . No matter how yy swaps, the difference between the number of black cells and the number of white cells is at least . It can be proven that there is no better solution.
For the second test case, youyou chooses the eight cells . No matter how yy swaps, the difference between the number of black cells and the number of white cells is at least . It can be proven that there is no better solution.
[Sample #2]
See the attached files summer/summer2.in and summer/summer2.ans.
This sample satisfies the constraints of subtasks .
[Sample #3]
See the attached files summer/summer3.in and summer/summer3.ans.
This sample satisfies the constraints of subtasks .
[Sample #4]
See the attached files summer/summer4.in and summer/summer4.ans。
For the first test case, it satisfies special property A.
For the second test case, it satisfies special properties B and C.
For the third test case, it satisfies special property B.
For the fourth test case, it satisfies special property C.
For the fifth test case, it satisfies special property D.
This sample satisfies and .
[Sample #5]
See the attached files summer/summer5.in and summer/summer5.ans。
This sample satisfies the constraints of subtasks .
[Constraints]
This problem has subtasks, each worth points.
| Subtask ID | Special Properties | |
|---|---|---|
| None | ||
| A | ||
| B and C | ||
| B | ||
| C | ||
| D | ||
| None |
Special property A: it is guaranteed that there is no column where the upper and lower cells are one black and one white.
Special property B: it is guaranteed that there is no column where both cells are black.
Special property C: it is guaranteed that there is no column where both cells are white.
Special property D: it is guaranteed that for any position, its color is generated randomly with equal probability of being black or white.
For all testdata, it is guaranteed that , , and 。
Translated by ChatGPT 5