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: 39ms
Memory: 5864kB
Author: 2017212212217
In contest: 1289

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int a[10005];
int cmp1(int x,int y)
{
    return x>y;
}
int ans[10005][105];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n,cmp1);
        int l,r;
        for(int i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                l=i;
            }
        }
        r=n/l;

        int d1=l,d2=r;
        int vew=1;
        for(int j=1;j<=(d2+1)/2;j++)
        {
            for(int i=j;i<=l;i++)
            {
                ans[j][i]=a[vew++];
            }
			if(vew==n+1)break;
            for(int i=j+1;i<=r;i++)
            {
                ans[i][l]=a[vew++];
            }
			if(vew==n+1)break;
            for(int i=l-1;i>=j;i--)
            {
                ans[r][i]=a[vew++];
            }
			if(vew==n+1)break;
            for(int i=r-1;i>j;i--)
            {
                ans[i][j]=a[vew++];
            }
			if(vew==n+1)break;
            l--;
            r--;
        }
        for(int i=1;i<=d2;i++)
        {
            for(int j=1;j<=d1;j++)
            {
                printf("%d",ans[i][j]);
                if(j!=d2)
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
		if(t)printf("\n");
    }
}