/*************
*Author:chanjun
*email:15755396353@163.com
*************/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define LL long long
int n,m;
int l;
int g[3500][3500];
int dir[][2] = {0,1,1,0,0,-1,-1,0};
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
scanf("%d",&l);
vector<int> a(l);
for (int i = 0; i < l; ++i){
scanf("%d",&a[i]);
}
int n = sqrt(l);
while (l % n){
--n;
}
m = l / n;
sort(a.begin(),a.end(),greater<int>());
int i = 0, j = 0;
int cnt = 0;
g[i][j] = a[cnt];
int flag = 0;
while (cnt+1 < l){
int ti = i + dir[flag][0];
int tj = j + dir[flag][1];
if (ti >= m || ti < 0 || tj >= n || tj < 0 || g[ti][tj]){
flag = (flag+1)%4;
}
i += dir[flag][0];
j += dir[flag][1];
g[i][j] = a[++cnt];
}
for (int i = 0; i < m; ++i){
for (int j = 0; j < n-1; ++j){
printf("%d ", g[i][j]);
}
printf("%d\n",g[i][n-1]);
}
system("pause");
return 0;
}
pat advanced 1105
最新推荐文章于 2024-06-30 16:11:30 发布