Palindromic Matrix
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.

For example, the following matrices are palindromic:

The following matrices are not palindromic because they change after the order of rows is reversed:

The following matrices are not palindromic because they change after the order of columns is reversed:

You are given n2n2 integers. Put them into a matrix of nn rows and nn columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print "NO".

Input

The first line contains one integer nn (1≤n≤201≤n≤20 ).

The second line contains n2n2 integers a1,a2,…,an2a1,a2,…,an2 (1≤ai≤10001≤ai≤1000 ) — the numbers to put into a matrix of nn rows and nn columns.

Output

If it is possible to put all of the n2n2 numbers into a matrix of nn rows and nn columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print "YES". Then print nn lines with nn space-separated numbers — the resulting matrix.

If it's impossible to construct any matrix, then print "NO".

You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

Examples

Input

Copy
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1
Output

Copy
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1
Input

Copy
3
1 1 1 1 1 3 3 3 3
Output

Copy
YES
1 3 1
3 1 3
1 3 1
Input

Copy
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1
Output

Copy
NO
Input

Copy
1
10
Output

Copy
YES
10
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std; #define forn(i,n) for( int i=0; i<n; i++ )
typedef pair<int,int> pt;
const int maxn =+;
int cnt[maxn];
int a[][]; int main(int argc, char const *argv[])
{
int n;
scanf("%d",&n);
forn(i,n*n){
int x;
scanf("%d",&x);
cnt[x]++;
}
vector<pair<int,pt> > cells;
forn(i,(n+)/) forn(j,(n+)/){
if(i!=n-i-&&j!=n-j-){
cells.push_back({,{i,j}});
}
else if((i!=n-i-)^(j!=n-j-)){
cells.push_back({,{i,j}});
}
else{
cells.push_back({,{i,j}});
}
}
for( auto cur : {,,}){
int lst=;
for( auto it : cells ){
if(it.first!=cur) continue;
int i=it.second.first;
int j=it.second.second;
while(lst<maxn&&cnt[lst]<cur){
lst++;
}
if(lst==maxn){
puts("NO");
return ;
}
a[i][j]=a[n-i-][j]=a[i][n-j-]=a[n-i-][n-j-]=lst;
cnt[lst]-=cur;
}
}
puts("YES");
forn(i,n){
forn(j,n){
printf("%d ",a[i][j]);
}
puts("");
}
return ;
}

Palindromic Matrix的更多相关文章

  1. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  2. CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)

    个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列 装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满 ...

  3. Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix

    https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...

  4. [Code Festival 2017 qual A] C: Palindromic Matrix

    题意 给出一个小写字母组成的字符矩阵,问能否通过重排其中的字符使得每行每列都是回文串. 分析 简化版:给出一个字符串,问能否通过重排其中的字符使得它是回文串.那么如果字符串长度为偶数,就需要a到z的个 ...

  5. CODE FESTIVAL 2017 qual A C Palindromic Matrix(补题)

    彩笔看到题目后,除了懵逼,没有啥反应了,唯一想的就是 这是不是dp啊?看了题解才发现,原来是这样啊. 画几个矩阵看看就能看出来规律. 思路:先假设这是个M * N的矩阵 如果M和N都是偶数,则每个出现 ...

  6. Codeforces Round #540 (Div. 3) C. Palindromic Matrix (大模拟)

    题意:给你\(n\)个数,判断是否能构成一个\(n\)X\(n\)的回文矩阵,若可以,输出\(YES\)和矩阵,否则输出\(NO\). 题解:如果这个矩阵的行/列元素是偶数的话,很好办,所有出现的数一 ...

  7. Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1

    A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...

  8. 【AtCoder】CODE FESTIVAL 2017 qual A

    A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...

  9. CODE FESTIVAL 2017 qual A 题解

    补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...

随机推荐

  1. wireshark解析自定义的protobuf协议

    先看最终效果 wireshark是开源的,而且在Windows下安装时用的是64位,所以相应的库文件需要使用64位. 一个Lua插件的Dissector结构大致如下: do -- 协议名称为 m_Me ...

  2. Android的Databinding-单向绑定

    两种方式实现观察绑定. 一.POJO类实现android.databinding.Observable,在属性set方法中调用notifyPropertyChanged(BR.lastName); 其 ...

  3. Kubernetes中资源配额管理

    设置资源请求数量 创建Pod的时候,可以为每个容器指定资源消耗的限制.Pod的资源请求限制则是Pod中所有容器请求资源的总和. apiVersion: v1 kind: Pod metadata: n ...

  4. C# Task WaitAll和WaitAny

    Task 有静态方法WaitAll和WaitAny,主要用于等待其他Task完成后做一些事情,先看看其实现部分吧: public class Task : IThreadPoolWorkItem, I ...

  5. 我的Linux操作系统的发行版是什么?版本号是什么?

    可以用下面的两个命令来查看: cat /etc/*-release hostnamectl 参考资料 ============== How To Find Out My Linux Distribut ...

  6. oracle显示一个月的所有天数

    效率高: ) s_date from dual connect 效率低: select to_date() s_date from all_tables where rownum <= ( fr ...

  7. MySQL体系结构图详解

    体系结构图如下: 连接层 思想为解决资源的频繁分配﹑释放所造成的问题,为数据库连接建立一个“缓冲池”.原理预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕 ...

  8. Gitlab团队协作流程

    一:加入群组 项目管理员添加用户到群组,赋予权限(owner.developer...). 二:拉取项目,建立分支 通过git clone拉取项目到本地,通过终端打开项目目录,创建自己的分支,并推送到 ...

  9. python数据分析实用小抄

    1. python数据分析基础 2. numpy 3. Scikit-Learn 4. Bokeh 5. Scipy 6. Pandas   转载于:http://www.jianshu.com/p/ ...

  10. Self-Host c#学习笔记之Application.DoEvents应用 不用IIS也能執行ASP.NET Web API

    Self-Host   寄宿Web API 不一定需要IIS 的支持,我们可以采用Self Host 的方式使用任意类型的应用程序(控制台.Windows Forms 应用.WPF 应用甚至是Wind ...