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: 69ms
Memory: 6152kB
Author: 2018212212317
In contest: 1289

#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int maxn = 10010;
const int inf = 0x3f3f3f3f;
const int modu = 1e9+7;
int n,m,k,t,p,q,x,y,ans;
int a[maxn];
int b[maxn][110];
int vis[maxn];
bool cmp(int a,int b){
	return a>b;
}
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n); 
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		sort(a,a+n);
		for(int j=(int)sqrt(n);j>0;j--){
			if(n%j==0){
				if(j>n/j){
					p=j;
					q=n/j;
					break;
				}
				else{
					p=n/j;
					q=j;
					break;
				}
			} 
		}
		memset(b,0,sizeof(b));
		int cnt=0;
		x=0;y=0;
		
		
		while(cnt<n){
			while(b[x][y]==0&&y<q){
				b[x][y]=a[n-1-cnt];
				cnt++;
				y++;
			}
			y--;
			x++;
			while(b[x][y]==0&&x<p){
				b[x][y]=a[n-1-cnt];
				cnt++;
				x++;
			}
			x--;
			y--;
			while(b[x][y]==0&&y>=0){
				b[x][y]=a[n-1-cnt];
				cnt++;
				y--;
			}
			y++;
			x--;
			while(b[x][y]==0&&x>=0){
				b[x][y]=a[n-1-cnt];
				cnt++;
				x--;
			}
			x++;
			y++;
		}
		
		
		for(int i=0;i<p;i++){
			for(int j=0;j<q;j++){
				if(j==q-1) printf("%d\n",b[i][j]);
				else printf("%d ",b[i][j]);
			}
		}
		printf("\n");
	}
}