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

Problem_ID: B
Result: Accepted
Time: 64ms
Memory: 6152kB
Author: 2018212212317
In contest: 1289

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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;
		
		int flag=1;
		while(cnt<n){
			b[x][y]=a[n-1-cnt];
			cnt++;
			if(flag==1){
				y++;
				if(y==q||b[x][y]!=0){
					y--;
					x++;
					flag=2;
				}
			}
			else if(flag==2){
				x++;
				if(x==p||b[x][y]!=0){
					x--;
					y--;
					flag=3;
				}
			}
			else if(flag==3){
				y--;
				if(y==-1||b[x][y]!=0){
					y++;
					x--;
					flag=4;
				}
			}
			else if(flag==4){
				x--;
				if(x==-1||b[x][y]!=0){
					y++;
					x++;
					flag=1;
				}
			}	
		}
		
		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");
	}
}