Palindromic Matrix
2 seconds
256 megabytes
standard input
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".
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.
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
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1
3
1 1 1 1 1 3 3 3 3
YES
1 3 1
3 1 3
1 3 1
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1
NO
1
10
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的更多相关文章
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
- CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)
个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列 装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满 ...
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- [Code Festival 2017 qual A] C: Palindromic Matrix
题意 给出一个小写字母组成的字符矩阵,问能否通过重排其中的字符使得每行每列都是回文串. 分析 简化版:给出一个字符串,问能否通过重排其中的字符使得它是回文串.那么如果字符串长度为偶数,就需要a到z的个 ...
- CODE FESTIVAL 2017 qual A C Palindromic Matrix(补题)
彩笔看到题目后,除了懵逼,没有啥反应了,唯一想的就是 这是不是dp啊?看了题解才发现,原来是这样啊. 画几个矩阵看看就能看出来规律. 思路:先假设这是个M * N的矩阵 如果M和N都是偶数,则每个出现 ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix (大模拟)
题意:给你\(n\)个数,判断是否能构成一个\(n\)X\(n\)的回文矩阵,若可以,输出\(YES\)和矩阵,否则输出\(NO\). 题解:如果这个矩阵的行/列元素是偶数的话,很好办,所有出现的数一 ...
- 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> ...
- 【AtCoder】CODE FESTIVAL 2017 qual A
A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...
- CODE FESTIVAL 2017 qual A 题解
补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...
随机推荐
- Map:目录
ylbtech-Map:目录 1.返回顶部 1.百度地图 http://lbsyun.baidu.com/ 2.高德地图 http://lbs.amap.com/ 3. 2.返回顶部 3. ...
- 浅谈MFC类CrackMe中消息处理函数查找方法
最近一个学姐发给我了一份CrackMe希望我解一下,其中涉及到了MFC的消息函数查找的问题,就顺便以此为例谈一下自己使用的消息函数查找的方法.本人萌新,如果有任何错漏与解释不清的地方,欢迎各路大佬指正 ...
- Centos7离线安装redis
1.1 下载.上传.解压redis4.0.11 下载地址:http://download.redis.io/releases/redis-4.0.11.tar.gz下载后上传到服务器上,并解压:备注: ...
- 关于IP,这里有你想知道的一切!
关于IP,这里有你想知道的一切! 原创: 同盾反欺诈研究院 先知安全技术社区 2017-07-13 今日,就来跟大家聊聊关于IP地址方方面面的研究,其实可以归到三个问题上: 1.这个IP在哪儿? 2. ...
- C#调用系统打印机和收银钱箱
打印示例: StringBuilder builder = new StringBuilder();builder.AppendLine("--------------打印测试------- ...
- mysqlpump 和 mysql_config_editor测试
The mysql_config_editor utility enables you to store authentication credentials in an obfuscated log ...
- JAVA与C#的区别
Java和C#都是编程的语言,它们是两个不同方向的两种语言 相同点: 他们都是面向对象的语言,也就是说,它们都能实现面向对象的思想(封装,继承,多态) 区别: 1.c#中的命名空间是namespace ...
- Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页
Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...
- Android Studio添加原生库并自动构建
[时间:2017-09] [状态:Open] [关键词:Android,Android Studio,gradle,native,c,c++,cmake,原生开发,ndk-build] 0 引言 最近 ...
- 【nodejs】初识 NodeJS(二)
上一节我们构建了一个基础的 http 服务器,我们可以接收 http 请求,但是我们得做点什么吧 – 不同的 http 请求,服务器应该有不同的响应. 路由模块 处理不同的 http 请求在我们的代码 ...