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 ...
随机推荐
- ArcGIS鼠标滚轮方向之代码篇
Desktop10.X有多个版本,不同版本的注册表路径不一致,注册表中可能残留多个版本的注册信息:也可能没有Desktop,而是Engine.其实可以通过RuntimeManager.ActiveRu ...
- C#模拟客户端发送数据示例
在给一些客户端做服务器端支持时,发现他们提交上来的数据大都不是http请求格式,因而使用Request.Form获取不到内容,今天用C#做下模拟,并演示下数据接收. 1.发送文本 1).客户端发送 ...
- DEVOPS落地实践分享
DEVOPS落地实践分享 转载本文需注明出处:微信公众号EAWorld,违者必究. 引言: DevOps的理念已经说了很多年,其带来的价值逐渐被接受,很多企业也逐渐引入了DevOps.目前普元DevO ...
- github和gitlab并存
原先在自己电脑中配置github的全局变量,只用它来玩github和码云的项目.现在用自己的电脑开发公司项目,公司项目使用的是gitlab,结果拉取代码时还需要手动输入用户名和密码,这还能忍受几次,但 ...
- Laravel: 基础篇
一.安装 1)采用一键安装包 http://laravelacademy.org/resources-download 2)Mac 上安装 ----------在Mac上安装composer----- ...
- Python之多线程和多进程
一.多线程 1.顺序执行单个线程,注意要顺序执行的话,需要用join. #coding=utf-8 from threading import Thread import time def my_co ...
- Xshell设置密钥登录CentOS6.5_64位(文字命令版)
1.新建/root/.ssh目录 mkdir /root/.ssh 2.创建authorized_keys文件 vi /root/.ssh/authorized_keys 3.复制公钥内容保存 :wq ...
- python itertools 模块讲解
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...
- Atitit r2017 r6 doc list on home ntpc.docx
Atitit r2017 r6 doc list on home ntpc.docx 驱动器 D 中的卷是 p2soft 卷的序列号是 9AD0-D3C8 D:\ati\r2017 v4 raf\r2 ...
- Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略、价值领先战略
Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略.价值领先战略 美国著名管理学家迈克尔•波特在<竞争战略>一书中提出了集中化战略和差异化战略.成本领先战略三种基本竞争战略,从 ...