释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间。
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m×nmust be equal to N; m≥n; and m−n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1000100;
int N,s[10010],mp[10010][10010];
int mi=inf,m,n;
int main(){
cin>>N;
for(int i=1;i<=N;i++)
cin>>s[i];
sort(s+1,s+1+N);
for(int i=1;i<=sqrt(N);i++){
if(N%i==0){
int ls=i;
if(abs(ls-N/ls)<mi){
n=max(ls,N/ls);
m=min(ls,N/ls);
}
}
}
// cout<<m<<' '<<n<<endl;
int x=0,y=1,d=1;
for(int i=N;i>=1;i--){
if(d==1){
x++;
if(x>m||mp[y][x]!=0){
d=2;
x--;
y++;
}
}
else if(d==2){
y++;
if(y>n||mp[y][x]!=0){
d=3;
y--;
x--;
}
}
else if(d==3){
x--;
if(x<1||mp[y][x]!=0){
d=4;
x++;
y--;
}
}
else{
y--;
if(y<1||mp[y][x]!=0){
d=1;
y++;
x++;
}
}
mp[y][x]=s[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<mp[i][j];
if(j!=m)
cout<<' ';
else
cout<<endl;
}
}
return 0;
}