题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John has a square canvas represented by an $N$ by $N$ grid of cells ($2 \leq N \leq 2000$, $N$ is even). He paints the canvas according to the following steps:
- First, he divides the canvas into four equal quadrants, separated by horizontal and vertical lines through the center of the canvas.
- Next, he paints a lovely painting in the top-right quadrant of the canvas. Each cell of the top-right quadrant will either be painted (represented by '#') or unpainted (represented by '.').
- Finally, since he is so proud of his painting, he reflects it across the previously mentioned horizontal and vertical lines into the other quadrants of the canvas.
For example, suppose $N=8$ and FJ painted the following painting in the top-right quadrant in step 2:
.#.. .#.. .##. ....
Then after reflecting across the horizontal and vertical lines into the other quadrants in step 3, the canvas would look as follows:
..#..#.. ..#..#.. .##..##. ........ ........ .##..##. ..#..#.. ..#..#..
However, while FJ was sleeping, Bessie broke into his barn and stole his precious canvas. She totally vandalized the canvas—removing some painted cells and adding more painted cells! Before FJ woke up, she returned the canvas to FJ.
FJ would like to modify his canvas so that it once again satisfies the reflective condition: that is, it is the result of reflecting the top-right quadrant into each of the other quadrants. Since he only has a limited number of resources, he would like to achieve this in as few operations as possible, where a single operation consists of either painting a cell or removing paint from a cell.
You are given the canvas after Bessie's vandalism, as well as a sequence of $U$ ($0\le U \leq 10^5$) updates to the canvas, each toggling a single cell to '.' if it is '#', or vice versa. Before any updates, and after each update, output the minimum number of operations $x$ FJ needs to perform so that the reflective condition is satisfied.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains integers $N$ and $U$.The next $N$ lines each contain $N$ characters representing the canvas after Bessie's vandalism. Every character is either '#' or '.'.
The following $U$ lines each contain integers $r$ and $c$, where $1 \leq r, c \leq N$, representing an update to the cell in the $r$th row from the top and $c$th column from the left of the canvas.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $U+1$ lines representing $x$ before any updates and after each update.SAMPLE INPUT:
4 5 ..#. ##.# #### ..## 1 3 2 3 4 3 4 4 4 4
SAMPLE OUTPUT:
4 3 2 1 0 1
The following canvas satisfies the reflective condition and differs from the original canvas by 4 operations:
.... #### #### ....
It is impossible to make the original canvas satisfy the reflective condition using fewer than 4 operations.
After updating $(1, 3)$, the canvas looks like this:
.... ##.# #### ..##
It now takes 3 operations to make the canvas satisfy the reflective condition.
After updating $(2, 3)$, the canvas looks like this:
.... #### #### ..##
It now takes 2 operations to make the canvas satisfy the reflective condition.
SCORING:
- Inputs 2-3: $N \le 4$
- Inputs 4-6: $U \le 10$
- Inputs 7-16: No additional constraints.
Problem credits: Chongtian Ma
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 2.2k平均输出 token(含思考) ↓
- 34.3输出速度 tokens/s ↑
- 65 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 4Pylint 问题 ↓
- 8.95Pylint 得分 ↑
- 6圈复杂度 ↓
- 45.4可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 4.8k平均输出 token(含思考) ↓
- 220.1输出速度 tokens/s ↑
- 22 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.14Pylint 得分 ↑
- 9圈复杂度 ↓
- 43.3可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 5.4k平均输出 token(含思考) ↓
- 192.4输出速度 tokens/s ↑
- 29 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.21Pylint 得分 ↑
- 11圈复杂度 ↓
- 59.5可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): input = sys.stdin.buffer.readline n, u = map(int, input().split()) grid = [bytearray(input().strip()) for _ in range(n)] h = n // 2 counts = bytearray(h * h) cost = (0, 1, 2, 1, 0) answer = 0 for i in range(h): row1 = grid[i] row2 = grid[n - 1 - i] base = i * h for j in range(h): opposite_j = n - 1 - j painted = ( (row1[j] == 35) + (row1[opposite_j] == 35) + (row2[j] == 35) + (row2[opposite_j] == 35) ) counts[base + j] = painted answer += cost[painted] output = [str(answer)] for _ in range(u): r, c = map(int, input().split()) r -= 1 c -= 1 group_r = min(r, n - 1 - r) group_c = min(c, n - 1 - c) index = group_r * h + group_c old_count = counts[index] if grid[r][c] == 35: grid[r][c] = 46 new_count = old_count - 1 else: grid[r][c] = 35 new_count = old_count + 1 counts[index] = new_count answer += cost[new_count] - cost[old_count] output.append(str(answer)) sys.stdout.write("\n".join(output) + "\n") if __name__ == "__main__": main()
import sys def main() -> None: data = sys.stdin.buffer N, U = map(int, data.readline().split()) M = N // 2 HASH = ord('#') DOT = ord('.') grid = bytearray(N * N) counts = [0] * (M * M) for r in range(N): line = data.readline().strip() start = r * N grid[start:start + N] = line rr = r if r < M else N - 1 - r base = rr * M for c in range(M): counts[base + c] += (line[c] == HASH) + (line[N - 1 - c] == HASH) cost = [0, 1, 2, 1, 0] total = 0 for k in counts: total += cost[k] answers = [str(total)] for _ in range(U): r, c = map(int, data.readline().split()) r -= 1 c -= 1 pos = r * N + c rr = r if r < M else N - 1 - r cc = c if c < M else N - 1 - c idx = rr * M + cc old_k = counts[idx] total -= cost[old_k] if grid[pos] == HASH: grid[pos] = DOT new_k = old_k - 1 else: grid[pos] = HASH new_k = old_k + 1 counts[idx] = new_k total += cost[new_k] answers.append(str(total)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def solve(): read = sys.stdin.buffer.readline n, u = map(int, read().split()) half = n // 2 last = n - 1 # Convert '.' to 0 and '#' to 1 for fast counting. translation = bytes.maketrans(b".#", b"\x00\x01") grid = [ bytearray(read().strip().translate(translation)) for _ in range(n) ] # Each reflection group is represented by its top-left cell. counts = bytearray(half * half) answer = 0 for i in range(half): top = grid[i] bottom = grid[last - i] base = i * half for j in range(half): painted = ( top[j] + top[last - j] + bottom[j] + bottom[last - j] ) counts[base + j] = painted answer += painted if painted <= 2 else 4 - painted output = [str(answer)] for _ in range(u): r, c = map(int, read().split()) r -= 1 c -= 1 # Canonical coordinates of this cell's reflection group. group_r = r if r < half else last - r group_c = c if c < half else last - c group_id = group_r * half + group_c old_value = grid[r][c] grid[r][c] = old_value ^ 1 old_count = counts[group_id] new_count = old_count + (1 if old_value == 0 else -1) counts[group_id] = new_count old_cost = old_count if old_count <= 2 else 4 - old_count new_cost = new_count if new_count <= 2 else 4 - new_count answer += new_cost - old_cost output.append(str(answer)) sys.stdout.write("\n".join(output) + "\n") if __name__ == "__main__": solve()