Start: Jul, 01, 2019 08:30:00
2019年度暑期短学期第四天
End: Jul, 01, 2019 11:30:00
Time elapsed:
Time remaining:

Problem_ID: B
Result: Accepted
Time: 42ms
Memory: 6112kB
Author: 2018212212163
In contest: 1277

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<map>
#include<queue>
#include<vector>
using namespace std;
int n,m,T,N,x,y,a[10010],b[10010][110];
int main()
{
	
	scanf("%d",&T);
	while(T--){
		scanf("%d",&N);
		for(int i=1;i<=N;i++) scanf("%d",&a[i]);
		sort(a+1,a+1+N);
		y=sqrt(N);
		for(y;y>=1;y--){
			if(N%y==0) break;
		}
		x=N/y;
		//printf("%d %d\n",x,y);
		int i=1,j=1,q=1;
		while(N>0){
			for(j=q;j<=y-q+1&&N>0;j++){
				b[i][j]=a[N--];
				//printf("%d,%d ",i,j);
			}
			j--;
			//printf("\n");
			for(i=q+1;i<=x-q+1&&N>0;i++){
				b[i][j]=a[N--];
				//printf("%d,%d ",i,j);
			}
			i--;
			//printf("\n");
			for(j=y-q;j>=q&&N>0;j--){
				b[i][j]=a[N--];
				//printf("%d,%d ",i,j);
			}
			j++;
			//printf("\n");
			for(i=x-q;i>=q+1&&N>0;i--){
				b[i][j]=a[N--];
				//printf("%d,%d ",i,j);
			}
			i++;
			//printf("\n");
			q++;
		}
		for(i=1;i<=x;i++){
			for(j=1;j<=y;j++){
				printf("%d",b[i][j]);
				if(j!=y) printf(" ");
			}
			printf("\n");
		}
		printf("\n");
	}
}