Start: Jun, 28, 2019 13:00:00
2019年度暑期短学期第四天-助教场
End: Jun, 30, 2019 23:59:00
Time elapsed:
Time remaining:

Problem_ID: B
Result: Accepted
Time: 41ms
Memory: 5772kB
Author: 2017212212167
In contest: 1289

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int maxn = 10010;

int matrix[maxn][102], a[maxn],t, N, temp;

bool cmp(int x, int y)
{
	return x > y;
}

int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &N);
		if (N == 1)
		{
			scanf("%d", &temp);
			printf("%d\n", temp);
			if (t != 0)
				puts("");

		}
		else
		{
			for (int i = 0; i <= N - 1; i++)
			{
				scanf("%d", &a[i]);
			}
			sort(a, a + N, cmp);
			int m = (int)ceil(sqrt(N*1.0));
			while (N % m != 0)
			{
				m++;
			}
			int n = N / m, i = 1, j = 1, now = 0, u = 1, d = m, l = 1, r = n;
			while (now < N)
			{
				while (now < N&&j < r)
				{
					matrix[i][j] = a[now++];
					j++;
				}
				while (now < N&&i < d)
				{
					matrix[i][j] = a[now++];
					i++;
				}
				while (now<N&&j>l)
				{
					matrix[i][j] = a[now++];
					j--;
				}
				while (now<N&&i>u)
				{
					matrix[i][j] = a[now++];
					i--;
				}
				u++, d--, l++, r--, i++, j++;
				if (now == N - 1)
				{
					matrix[i][j] = a[now++];
				}
			}
			for (int i = 1; i <= m; i++)
			{
				for (int j = 1; j <= n; j++)
				{
					if (j == 1)
						printf("%d", matrix[i][j]);
					else
						printf(" %d", matrix[i][j]);
				}
				puts("");
			}
			if (t != 0)
				puts("");
		}
	}
	return 0;
}