#P17022. [ROI 2026 Day2] 广义象棋

    ID: 19311 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2026ROI(俄罗斯)

[ROI 2026 Day2] 广义象棋

Problem Description

Mikhail decided to learn generalized chess, so he prepared a board of size n×nn \times n. He painted the cell in row ii and column jj with color aija_{i j}.

Since Mikhail is a beginner, he may have painted the board incorrectly. Therefore, some cells on the board may need to be repainted to another color. A board is called properly colored if and only if it satisfies both of the following conditions:

  • The board uses at most two different colors in total.
  • There are no edge-adjacent cells with the same color.

Mikhail also considers that playing on a board that is too large would be too difficult. Therefore, he may cut out a smaller board from his board, that is, keep the rectangle formed by the first rr rows and the first cc columns, and only make this part properly colored.

For every pair (r,c)(r, c) (1≤r≤n1 \le r \le n, 1≤c≤n1 \le c \le n), compute the value brcb_{r c}—the minimum number of cells Mikhail needs to repaint so that the rectangle formed by the first rr rows and the first cc columns becomes properly colored.

Input Format

The first line contains an integer nn (1≤n≤4001 \le n \le 400), which denotes the size of the board.

The next nn lines describe the board: the ii-th line contains nn integers ai1,…,aina_{i 1}, \ldots, a_{i n} (1≤aij≤1091 \le a_{i j} \le 10^9), representing the colors of the cells in row ii of the board.

Output Format

Output nn lines, where the ii-th line should contain nn integers bi1,…,binb_{i 1}, \ldots, b_{i n}.

2
7 7
7 7
0 1
1 2
3
1 1 2
2 4 4
3 1 2
0 1 1
0 2 4
1 3 5

Hint

Subtasks

Subtask Score nn aija_{i j} Dependencies
1 11 n≤50n \le 50 --
2 22 n≤200n \le 200 1
3 8 -- aij≤2a_{i j} \le 2 --
4 17 aij≤10a_{i j} \le 10 3
5 15 aij≤100a_{i j} \le 100 3–4
6 7 aij≤104a_{i j} \le 10^4 3–5
7 20 -- 1–6

Translated by DeepSeek V4 Pro.

Translated by ChatGPT 5