-
Notifications
You must be signed in to change notification settings - Fork 2
/
1118C.cpp
103 lines (90 loc) · 2.06 KB
/
1118C.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN
using namespace std;
map<int,int> m;
int onec,twoc;
int main()
{
int n,c,imp=0,o; scanf("%d",&n);
if(n%2) onec=1,twoc=2*(n/2);
FOR(i,0,n*n) scanf("%d",&c),m[c]++;
stack<int> fours,twos;
for(pii p: m)
{
if(p.se%4==1)
{
if(!onec) { imp=1; break; }
if(p.se/4>0) fours.push(p.fi);
onec=0; o=p.fi;
}
else if(p.se%4==2)
{
if(!twoc) { imp=1; break;}
if(p.se/4>0) fours.push(p.fi);
twos.push(p.fi);
twoc--;
}
else if(p.se%4==3)
{
if(!onec || !twoc) { imp=1; break; }
if(p.se/4>0) fours.push(p.fi);
twos.push(p.fi);
twoc--; onec=0; o=p.fi;
}
else if(p.se%4==0)
{
fours.push(p.fi);
}
}
if(imp)
{
printf("NO\n");
}
else
{
printf("YES\n");
int mat[n][n];
FOR(i,0,n/2)
{
FOR(j,0,n/2)
{
c=fours.top();
mat[i][j]=mat[i][n-j-1]=mat[n-i-1][n-j-1]=mat[n-i-1][j]=c;
m[c]-=4;
if(m[c]<4) fours.pop();
}
}
if(n%2)
{
while(!fours.empty()) { twos.push(fours.top()); fours.pop(); }
FOR(i,0,n/2)
{
c=twos.top();
mat[i][n/2]=mat[n-i-1][n/2]=c;
m[c]-=2;
if(m[c]<2) twos.pop();
}
FOR(i,0,n/2)
{
c=twos.top();
mat[n/2][i]=mat[n/2][n-i-1]=c;
m[c]-=2;
if(m[c]<2) twos.pop();
}
mat[n/2][n/2]=o;
}
FOR(i,0,n)
{
FOR(j,0,n)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
return 0;
}