任意门:http://codeforces.com/contest/1118/problem/C

C. 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
Note

Note that there exist multiple answers for the first two examples.

题意概括:

用一串序列构造一个回文方阵。

解题思路:

纯暴力。。。蜜汁bug;

做法很显然啊,分奇偶;

N为偶数的情况,直接按照策略四个四个构造即可,不存在解的情况就是出现不能被 4 整除的情况(上下左右要对称嘛);

N为奇数的情况,首先把最中间那个填了,找 出现次数为奇数的数即可。

然后也是先按照策略四个四个构造;

最后在两个两个构造,把中间那个十字部分填满。

想法很simple,bug很崩溃。。。

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e3+;
int sum[MAXN];
int ans[][];
int n, num; int main()
{
scanf("%d", &n);
int N = n*n, x;
for(int i = ; i <= N; i++){
scanf("%d", &x);
sum[x]++;
}
int flag = true;
int mid = (n+)/;
int num = ;
if(n% == ){
for(int i = ; i <= mid; i++){
for(int j = ; j <= mid; j++){
for(; num <= ; num++){
if(sum[num]){
if(sum[num]%){
flag = false;
break;
}
ans[i][j] = ans[i][n-j+] = ans[n-i+][j] = ans[n-i+][n-j+] = num;
sum[num]-=;
break;
}
}
if(num > ) flag = false;
if(!flag) break;
}
if(!flag) break;
}
}
else{
flag = true;
num = ;
for(; num <= ; num++){
if(sum[num]%){
ans[mid][mid] = num;
sum[num]--;
break;
}
}
if(num > ){
flag = false;
//puts("zjy");
}
else{
num = ;
for(int i = ; i < mid; i++){
for(int j = ; j < mid; j++){
for(; num <= ; num++){
if(sum[num] == ) continue;
if(sum[num]/ < ) continue;
ans[i][j] = ans[i][n-j+] = ans[n-i+][j] = ans[n-i+][n-j+] = num;
sum[num]-=;
break;
}
if(num > ) flag = false;
if(!flag) break;
}
if(!flag) break;
} num = ;
for(int i = ; i < mid; i++){
for(; num <= ; num++){
if(sum[num]/ >= ){
ans[mid][i] = ans[mid][n-i+] = num;
sum[num]-=;
break;
}
}
if(num > ){flag = false; break;}
} num = ;
for(int i = ; i < mid; i++){
for(; num <= ; num++){
if(sum[num]/ >= ){
ans[i][mid] = ans[n-i+][mid] = num;
sum[num]-=;
break;
}
}
if(num > ){flag = false; break;}
}
}
} if(!flag) puts("NO");
else{
puts("YES");
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++)
printf("%d ", ans[i][j]);
puts("");
}
} return ;
}

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】的更多相关文章

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

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

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

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

  3. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  4. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

  5. 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> ...

  6. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  7. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  8. Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)

    https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...

  9. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

随机推荐

  1. spring的事务管理配置

    spring有两种事务配置器,可以使用spring的jdbc事务管理器,也可以使用对hibernate的事务管理器 第一种 使用Spring JDBC或IBatis进行事务配置(配置文件方式): &l ...

  2. 域对象中属性变更及感知session绑定的事件监听器

    域对象中属性的变更的时间监听器就是用来监听ServletContext,HttpSession,HttpServletRequest这三个对象中的属性变更信息事件的监听器.这三个监听器接口分别是Ser ...

  3. TCP基础知识(一)简介与数据包

    TCP详解(1):简介与数据包 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 应用层向TCP层发送用于网间传输 ...

  4. Windows Server 2008 R2 搭建网站详细教程

    转自:http://jingyan.baidu.com/album/642c9d34098bf5644a46f71f.html?picindex=4 网上都有一些Windows Server 2008 ...

  5. nodejs项目目录结构规范

    参考: https://github.com/jifeng/node-app dispatch.js 主进程文件 worker.js 工作进程 app.js 应用 routes.js url路由表 p ...

  6. 定时器实现方式之TimerTask、Timer

    在未来某个指定的时间点或者经过一段时间延迟后执行某个事件,这时候就需要用到定时器了.定时器的实现方式有很多种,今天总结最简单的实现方式.java 1.3引入了定时器框架,用于在定时器上下文中控制线程的 ...

  7. window.open在Safari中不能打开的问题

    在调移动支付问题的时候遇到过,用window.open打开一个微信支付链接,唤醒移动支付,在IOS下死活唤醒不了,是js代码冲突问题...是click事件IOS下不兼容问题...最后定位到window ...

  8. js获取当前日期和时间的代码

    最佳答案 var myDate = new Date(); myDate.toLocaleDateString(): //获取当前日期myDate.toLocaleTimeString(); //获取 ...

  9. js小数乘法精确率问题

    研究拓扑图百分比乘法计算,带小数位计算会出现值溢出的问题 JS里做小数的乘法运算时会出现浮点错误:  结果是251.89999999999998 而不是251.9  这个问题想必有很多人为之头痛. 那 ...

  10. CentOS 7运维管理笔记(12)----PHP页面失去焦点后变成空白的解决方法

    昨天搭建好了LAMP服务器,可以正常看到PHP页面了.后来发现每当把鼠标从浏览器中移开而点击其他地方时,PHP页面就变成一片空白.即PHP页面失去焦点后就变空白,不知为何. 今天网上搜索解决方案,终于 ...