任意门: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. ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...

  2. HDU 1561 The more, The Better 经典树形DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. SQL Join 语句

    SQL Join 语句 SQL 中每一种连接操作都包括一个连接类型和连接条件. 连接类型 决定了如何处理连接条件不匹配的记录. 连接类型 返回结果 inner join 只包含左右表中满足连接条件的记 ...

  4. 全面理解Java内存模型(JMM)及volatile关键字(转)

    原文地址:全面理解Java内存模型(JMM)及volatile关键字 关联文章: 深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型( ...

  5. 接口调用 读取图片报错 Access to the path '' is denied.解决方案

    调用接口 读取服务器上 图片 报错: Server was unable to process request. ---> Access to the path '图片路径' is denied ...

  6. JavaScript的进阶之路(一)

    JavaScript由ECMAScript BOM DOM三部分组成 ECMAScript重要版本1,3,5,6,提供核心语言功能 DOM提供访问和操作网页内容的方法和接口 BOM提供与浏览器交互的的 ...

  7. 移动端mate标签设置

    <meta name="viewport" content="width=device-width,height=device-height,initial-sca ...

  8. SQL Server中的游标CURSOR

    游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: ...

  9. 解决PUTTY出现中文乱码问题

    Putty在默认情况下没有使用UTF-8编码,因此在显示中文的时候会出现乱码.解决方法非常简单:按照下图,在连接之前,左边窗口中的“转换”选项卡(如图所示),然后在右边“假定接收的数据字符集”的下拉选 ...

  10. Java 之数组(4)

    什么是数组: 问:编写代码保存 4 名学生的考试成绩. 答:简单啊,定义 4 个变量呗 问:那“计算全年级 400 名学生的考试成绩”,肿么办 答: ....... 数组,就可以帮助你妥妥的解决问题啦 ...