题目 › usaco-1472

Table Recovery

USACO 2025 January · 银组 · 2025-01 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Bessie has an $N\times N$ ($1\le N\le 1000$) addition table where the integer in the cell at row $r$ and column $c$ is $r+c$, for all $1\le r,c\le N$. For example, for $N=3$, the table would look like this:

2 3 4
3 4 5
4 5 6

Unfortunately, Elsie got ahold of the table and permuted it by performing the following three types of operations as many times as she wanted.

  1. Swap two rows
  2. Swap two columns
  3. Select two values $a$ and $b$ that are both present in the table, then simultaneously change every occurrence of $a$ to $b$ and every occurrence of $b$ to $a$.

Elsie will always perform operations in increasing order of type; that is, she performs as many operations as she likes (possibly none) first of type $1$, then of type $2$, and finally of type $3$.

Help Bessie recover a possible state of the table after Elsie finished applying all of her operations of types $1$ and $2$, but before applying any operations of type $3$. There may be multiple possible answers, in which case you should output the lexicographically smallest one.

To compare two tables lexicographically, compare the first entries at which they differ, when reading both tables in the natural order (rows from top to bottom, left to right within a row).

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$.

The next $N$ lines each contain $N$ integers, representing Bessie's addition table after Elsie has permuted it.

OUTPUT FORMAT (print output to the terminal / stdout):

The lexicographically minimum possible state of the table after all operations of types 1 and 2, but before any operations of type 3. It is guaranteed that the answer exists.

SAMPLE INPUT:

1
2

SAMPLE OUTPUT:

2

Regardless of what operations Elsie performs, the table won't change.

SAMPLE INPUT:

3
3 4 2
5 2 3
6 3 5

SAMPLE OUTPUT:

4 2 3
5 3 4
6 4 5

Here is a possible sequence of operations Elsie might have performed.

2 3 4
3 4 5
4 5 6
-> (op 1: swap columns 2 and 3)
2 4 3
3 5 4
4 6 5
-> (op 1: swap columns 1 and 2)
4 2 3
5 3 4
6 4 5
-> (op 3: swap values 2 and 3)
4 3 2
5 2 4
6 4 5
-> (op 3: swap values 3 and 4)
3 4 2
5 2 3
6 3 5

Note: the following is also a possible state of the table after operations of types 1 and 2, but it is not the lexicographically smallest because the second entry of the first row is larger than in the correct answer.

4 6 5
3 5 4
2 4 3

SAMPLE INPUT:

6
8 10 5 6 7 4
12 11 10 4 8 2
5 4 6 7 9 8
10 2 4 8 5 12
6 8 7 9 3 5
4 12 8 5 6 10

SAMPLE OUTPUT:

7 5 8 9 10 6
4 2 5 6 7 3
8 6 9 10 11 7
5 3 6 7 8 4
9 7 10 11 12 8
6 4 7 8 9 5

SCORING:

  • Inputs 4-5: $N\le 6$
  • Inputs 6-7: $N\le 8$
  • Inputs 8-11: $N\le 100$
  • Inputs 12-15: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 8.4k平均输出 token(含思考) ↓
  • 30.2输出速度 tokens/s ↑
  • 279 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 8.94Pylint 得分 ↑
  • 18圈复杂度 ↓
  • 62.5可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 20.4k平均输出 token(含思考) ↓
  • 197.4输出速度 tokens/s ↑
  • 104 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 9.10Pylint 得分 ↑
  • 29圈复杂度 ↓
  • 48.3可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 33.2k平均输出 token(含思考) ↓
  • 172.1输出速度 tokens/s ↑
  • 194 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 9.25Pylint 得分 ↑
  • 24圈复杂度 ↓
  • 54.5可维护性指数 ↑

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 66 行
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())

    table = []
    freq = [0] * (2 * n + 1)

    for _ in range(n):
        row = list(map(int, input().split()))
        table.append(row)
        for value in row:
            freq[value] += 1

    # A value occurring once must represent either 2 or 2*N.
    anchor_row = anchor_col = 0
    found = False
    for i in range(n):
        for j in range(n):
            if freq[table[i][j]] == 1:
                anchor_row, anchor_col = i, j
                found = True
                break
        if found:
            break

    # Treat the selected singleton as 2. Its row and column reveal the
    # original row and column labels through the value frequencies.
    row_labels = [freq[table[i][anchor_col]] for i in range(n)]
    col_labels = [freq[table[anchor_row][j]] for j in range(n)]

    del table

    # The only other possible table replaces every entry x by 2*N+2-x.
    # Determine which of the two is lexicographically smaller.
    middle = n + 1
    complement = False
    decided = False

    for r in row_labels:
        for c in col_labels:
            value = r + c
            if value != middle:
                complement = value > middle
                decided = True
                break
        if decided:
            break

    output = []
    if complement:
        total = 2 * n + 2
        for r in row_labels:
            base = total - r
            output.append(" ".join(str(base - c) for c in col_labels))
    else:
        for r in row_labels:
            output.append(" ".join(str(r + c) for c in col_labels))

    sys.stdout.write("\n".join(output) + "\n")


if __name__ == "__main__":
    main()