CF1713E.Cross Swapping

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a square matrix AA of size n×nn \times n whose elements are integers. We will denote the element on the intersection of the ii-th row and the jj-th column as Ai,jA_{i,j}.

You can perform operations on the matrix. In each operation, you can choose an integer kk, then for each index ii (1≤i≤n1 \leq i \leq n), swap Ai,kA_{i, k} with Ak,iA_{k, i}. Note that cell Ak,kA_{k, k} remains unchanged.

For example, for n=4n = 4 and k=3k = 3, this matrix will be transformed like this:

The operation k=3k = 3 swaps the blue row with the green column.

You can perform this operation any number of times. Find the lexicographically smallest matrix†^\dagger you can obtain after performing arbitrary number of operations.

†{}^\dagger For two matrices AA and BB of size n×nn \times n, let a(i−1)⋅n+j=Ai,ja_{(i-1) \cdot n + j} = A_{i,j} and b(i−1)⋅n+j=Bi,jb_{(i-1) \cdot n + j} = B_{i,j}. Then, the matrix AA is lexicographically smaller than the matrix BB when there exists an index ii (1≤i≤n21 \leq i \leq n^2) such that ai<bia_i \lt b_i and for all indices jj such that 1≤j<i1 \leq j \lt i, aj=bja_j = b_j.

给你一个 n×nn \times n 的方阵 AA,其元素均为整数。我们用 Ai,jA_{i,j} 表示第 ii 行与第 jj 列交叉处的元素。

你可以对矩阵执行若干次操作。每次操作中,你可任选一个整数 kk,然后对每个下标 ii(1≤i≤n1 \leq i \leq n),交换 Ai,kA_{i, k} 与 Ak,iA_{k, i}。注意,元素 Ak,kA_{k, k} 保持不变。

例如,当 n=4n = 4 且 k=3k = 3 时,该矩阵将按如下方式变换:

操作 k=3k = 3 将蓝色行与绿色列互换。

你可以执行该操作任意多次。求经过任意次操作后所能得到的字典序最小的矩阵†^\dagger。

†{}^\dagger 对于两个 n×nn \times n 矩阵 AA 和 BB,定义 a(i−1)⋅n+j=Ai,ja_{(i-1) \cdot n + j} = A_{i,j},b(i−1)⋅n+j=Bi,jb_{(i-1) \cdot n + j} = B_{i,j}。则称矩阵 AA 字典序小于矩阵 BB,当且仅当存在某个下标 ii(1≤i≤n21 \leq i \leq n^2),使得 ai<bia_i \lt b_i,且对所有满足 1≤j<i1 \leq j \lt i 的下标 jj,均有 aj=bja_j = b_j。

输入格式

The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤10001 \leq n \leq 1000) — the size of the matrix.

The ii-th line of the next nn lines contains nn integers Ai,1,Ai,2,…,Ai,nA_{i, 1}, A_{i, 2}, \dots, A_{i, n} (1≤Ai,j≤1091 \le A_{i, j} \le 10^9) — description of the matrix AA.

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 10610^6.

第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤10001 \leq n \leq 1000)—— 矩阵的大小。

接下来 nn 行中的第 ii 行包含 nn 个整数 Ai,1,Ai,2,…,Ai,nA_{i, 1}, A_{i, 2}, \dots, A_{i, n}(1≤Ai,j≤1091 \le A_{i, j} \le 10^9)—— 矩阵 AA 的描述。

保证所有测试用例的 n2n^2 之和不超过 10610^6。

输出格式

For each test case, print nn lines with nn integers each — the lexicographically smallest matrix.

对于每个测试用例,输出 nn 行,每行包含 nn 个整数——即字典序最小的矩阵。

输入输出样例

  • 输入#1

    2
    3
    2 1 2
    2 1 2
    1 1 2
    4
    3 3 1 2
    1 1 3 1
    3 2 3 2
    2 3 3 1

    输出#1

    2 1 1
    2 1 1
    2 2 2
    3 1 1 2
    3 1 2 1
    3 3 3 3
    2 3 2 1

说明/提示

Note that in every picture below the matrix is transformed in such a way that the blue rows are swapped with the green columns.

In the first test case, we can perform 11 operation for k=3k = 3. The matrix will be transformed as below:

In the second test case, we can perform 22 operations for k=1k = 1 and k=3k = 3:

注意:在下方每张图中,矩阵均经过变换,使得蓝色行与绿色列互换。

在第一个测试用例中,我们可以对 k=3k = 3 执行 11 次操作。矩阵将按如下方式变换:

在第二个测试用例中,我们可以分别对 k=1k = 1 和 k=3k = 3 各执行 11 次操作,共 22 次操作:

输入解题思路,AI测评打分。不知道怎么写?

首页