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 ...
随机推荐
- Elastic{ON}参会随手记
Elastic{ON} 同事送了一张Elastic{ON}的票,因为我们的产品中用到的ELK全家桶,实话说用的体量还挺大的,因此非常想去参加这次的发布会. 7.0的新特性 上午的会议是来自总部的一名工 ...
- 条件式变分自编码机(Conditional Variational Autoencoders)
Conditional Variational Autoencoders 条件式变换自编码机 摘要: Conditional Variational Autoencoders --- 条件式变换自编码 ...
- double compare 0
因为double类型或float类型都是有精度的,其实都是取的近似值,所以有个误差.和一个很小的数比如0.00000001(1e-8)比较就是为了在这个误差范围内进行比较. 举个例子如double b ...
- 命令 上传项目到git中
点击Clone or dowload会出现一个地址,copy这个地址备用. 接下来就到本地操作了,首先右键你的项目,如果你之前安装git成功的话,右键会出现两个新选项,分别为Git Gui Here, ...
- 【Tomcat】Tomcat报错追踪
应用部署上之后无法正常启动.tomcat启动日志例如以下: 信息: Starting service Catalina 2015-7-22 18:39:31 org.apache.catalina.c ...
- 微信支付 python版
需求: 微信打开商品列表页面-> 点击商品后直接显示付款页面-> 点击付款调用微信支付 说明 微信支付需要你申请了公众号(appid, key - 用于签名), 商户号(mch_id, A ...
- Java远程调用原理DEMO
1. POJO public class DemoInfo implements Serializable{ private String name; private int age; public ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- Unty中通过镜像优化HDRI全景图体积
全景图即HDRI贴图,可以代替6面cubemap,传统3D软件运用较为广泛.一般反射探针,天空盒等都会用到. 但是体积过大是个问题,特别是移动端会对包体大小进行控制,虽说可以通过球面贴图替换掉部分环境 ...
- Asp.Net WebApi接口返回值IHttpActionResult
WebApi是微软在VS2012 MVC4版本中绑定发行的,webapi2.0同mvc5发行的 webapi一共有以下接口返回值 1.void无返回值2.IHttpActionResult Json( ...