time limit per test
2 seconds
memory limit per test
256 megabytes
Brr Brrr Patapim is trying to learn of Tiramisù's secret passcode, which is a permutation∗ of 2⋅n elements. To help Patapim guess, Tiramisù gave him an n×n grid G, in which Gi,j (or the element in the i-th row and j-th column of the grid) contains pi+j, or the (i+j)-th element in the permutation.
Given this grid, please help Patapim crack the forgotten code. It is guaranteed that the permutation exists, and it can be shown that the permutation can be determined uniquely.
∗A permutation of m integers is a sequence of m integers which contains each of 1,2,...,m exactly once. For example, 1,3,2 and 2,1 are permutations, while 1,2,4 and 1,3,2,3 are not.
Input
The first line contains an integer t --- the number of test cases (1≤t≤200).
The first line of each test case contains an integer n (1≤n≤800).
Each of the following n lines contains n integers, giving the grid G. The first of these lines contains G1,1,G1,2,...,G1,n; the second of these lines contains G2,1,G2,2,...,G2,n, and so on. (1≤Gi,j≤2⋅n).
It is guaranteed that the grid encodes a valid permutation, and the sum of n over all test cases does not exceed 800.
Output
For each test case, please output 2n numbers on a new line: p1,p2,...,p2n.
Example
Input
Copy
3
3
1 6 2
6 2 4
2 4 3
1
1
2
2 3
3 4
Output
Copy
5 1 6 2 4 3
2 1
1 2 3 4
解题说明:此题是一道数学题,题目给一个 n × n 的网格 G,其中 G[i][j] 等于一个长度为 2n 的排列 p 中第 (i + j) 个元素的值。也就是说,网格暴露了 p[2] 到 p[2n] 的所有值,但 p[1] 没有直接给出。需要还原整个排列 p。因为 p 是 1 到 2n 的排列,所以只要用数组 b 标记网格中出现过的数字,再从 1 到 2n 扫一遍,第一个没被标记的就是 p[1]。
cpp
#include<stdio.h>
int main()
{
int t;
scanf("%d", &t);
int a[850][850];
while (t--)
{
int n = 0;
int b[4005] = { 0 };
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
b[a[i][j]] = 1;
}
}
for (int i = 1; i <= 2 * n; i++)
{
if (b[i] == 0)
{
printf("%d ", i);
break;
}
}
int i = 1, j = 1, count = 1;
while (i <= n && j <= n)
{
printf("%d ", a[i][j]);
if (count % 2 == 0)
{
j++;
}
else
{
i++;
}
count++;
}
printf("\n");
}
return 0;
}