C. Brr Brrr Patapim

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;
}
相关推荐
山楂树の2 小时前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
xurileidian3 小时前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
FRing033 小时前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
syf0611133 小时前
Java Math 类详解:常用数学运算方法全解析
java·开发语言
是main不是漫4 小时前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
老花眼猫4 小时前
C语言绘图方法:(24)鼓形缩放旋转
c语言·经验分享·青少年编程·课程设计
慧都产品智能化4 小时前
汽车软件年底自查:按安全影响排定缺陷整改顺序
c语言·c++·测试工具·单元测试·汽车·代码规范
geovindu5 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
Zwawa5 小时前
USB 与有线接口:插上去之后,这条线上发生了什么
c语言·单片机·嵌入式
星夜离尘566 小时前
MATLAB:两路读数平均相差 8 mV,换位能分出传感器偏差和通道偏差吗?
开发语言·matlab