任意门: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. 什么是web service (转)

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  2. (原创).Net将EF运用于Oralce一 准备工作

    网上有很多EF运用于Oracle的博文,但是找了半天发现大多数博文大都语焉不详,于是决定自己折腾. 首先我的开发工具为vs2010,那么最适用于VS2010的EF版本为多少呢?答案是EF5.我在Sta ...

  3. 在 Azure 上创建和链接 MySQL 数据库

    本快速入门介绍了如何使用 Azure 门户创建并连接 MySQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前如果您还没有 Azure 账户,可以申请 1 元试用账户 步骤1:创 ...

  4. java 的底层通信--Socket

    以前一直不太重视java 基础的整理,感觉在实际开发中好像java 基础用处不大,感觉不理解一些底层的东西对开发工作影响也不大.不过,后来我发现,很多东西都是相互联系的,如果底层的东西你不理解,后面的 ...

  5. springboot集成邮件服务

    一.前言 Spring Email 抽象的核心是 MailSender 接口,MailSender 的实现能够把 Email 发送给邮件服务器,由邮件服务器实现邮件发送的功能. Spring 自带了一 ...

  6. Spring系列之Alias标签的解析与使用

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. VirtualBox使用Centos7与主机共享文件夹

    最近使用VitrtualBox安装Centos7学习,liunx脚本和一些命令,经过一些研究完成了虚拟机与 主机共享文件夹,虚拟机链接外部网络,主机与虚拟机互相通信.在其中遇到一些我解决的技术问题记录 ...

  8. lincode 题目记录5

    Course Schedule 安排课表   Frog Jump  最长回文字符串长度 Course Schedule 选课方案问题,题目说的很清楚了就是bfs或者dfs,然后加个字典优化,弄了好久没 ...

  9. My SQL查询语言

    基础查询 一.语法select 查询列表from 表名;二.特点1.查询列表可以是字段.常量.表达式.函数,也可以是多个2.查询结果是一个虚拟表 三.示例1.查询单个字段select 字段名 from ...

  10. 转 linux screen 命令详解

    一.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...