A15359 | Cross Swapping
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a square matrix $A$ of size $n \times n$ whose elements are integers. We will denote the element on the intersection of the $i$ -th row and the $j$ -th column as $A_{i,j}$ .
You can perform operations on the matrix. In each operation, you can choose an integer $k$ , then for each index $i$ ( $1 \leq i \leq n$ ), swap $A_{i, k}$ with $A_{k, i}$ . Note that cell $A_{k, k}$ remains unchanged.
For example, for $n = 4$ and $k = 3$ , this matrix will be transformed like this:
The operation $k = 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 $A$ and $B$ of size $n \times n$ , let $a_{(i-1) \cdot n + j} = A_{i,j}$ and $b_{(i-1) \cdot n + j} = B_{i,j}$ . Then, the matrix $A$ is lexicographically smaller than the matrix $B$ when there exists an index $i$ ( $1 \leq i \leq n^2$ ) such that $a_i < b_i$ and for all indices $j$ such that $1 \leq j < i$ , $a_j = b_j$ .
You can perform operations on the matrix. In each operation, you can choose an integer $k$ , then for each index $i$ ( $1 \leq i \leq n$ ), swap $A_{i, k}$ with $A_{k, i}$ . Note that cell $A_{k, k}$ remains unchanged.
For example, for $n = 4$ and $k = 3$ , this matrix will be transformed like this:
The operation $k = 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 $A$ and $B$ of size $n \times n$ , let $a_{(i-1) \cdot n + j} = A_{i,j}$ and $b_{(i-1) \cdot n + j} = B_{i,j}$ . Then, the matrix $A$ is lexicographically smaller than the matrix $B$ when there exists an index $i$ ( $1 \leq i \leq n^2$ ) such that $a_i < b_i$ and for all indices $j$ such that $1 \leq j < i$ , $a_j = b_j$ .
输入格式
The first line contains a single integer $t$ ( $1 \leq t \leq 10^5$ ) — the number of test cases.
The first line of each test case contains a single integer $n$ ( $1 \leq n \leq 1000$ ) — the size of the matrix.
The $i$ -th line of the next $n$ lines contains $n$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, n}$ ( $1 \le A_{i, j} \le 10^9$ ) — description of the matrix $A$ .
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $10^6$ .
The first line of each test case contains a single integer $n$ ( $1 \leq n \leq 1000$ ) — the size of the matrix.
The $i$ -th line of the next $n$ lines contains $n$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, n}$ ( $1 \le A_{i, j} \le 10^9$ ) — description of the matrix $A$ .
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $10^6$ .
输出格式
For each test case, print $n$ lines with $n$ integers each — the lexicographically smallest matrix.
输入输出样例
输入 #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 $1$ operation for $k = 3$ . The matrix will be transformed as below:
 In the second test case, we can perform $2$ operations for $k = 1$ and $k = 3$ :  
In the first test case, we can perform $1$ operation for $k = 3$ . The matrix will be transformed as below:
 In the second test case, we can perform $2$ operations for $k = 1$ and $k = 3$ :  
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted