任意门: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. canvas绘制经典星空连线效果

    来自:https://segmentfault.com/a/1190000009675230 下面开始coding:先写个canvas标签 <canvas height="620&qu ...

  2. 《Maven实战》关联实际工作的核心知识

    通读了<Maven实战>这本书,由于在实际的工作中,对其有一定的操作上的经验.因此,再回头去通读这本书,就能够更加精准的把握里面的核心知识了. 以下我主要从两点去介绍之—— 1> m ...

  3. [PHP] Oauth授权和本地加密

    1.Oauth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方 关键字:appKey appSecre ...

  4. springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页、个人作品、个人博客)

    前言 My Site 主要功能有:个人首页.个人作品.个人博客为一体的站点,网站的文章和作品均由markdown进行编写,可以满足你的基本需求.如果觉得这个项目不错,请为它点赞支持. 项目架构 JDK ...

  5. c语言printf实现同一位置打印输出

    控制台同一位置打印输出,例如:进度1%->100%在同一位置显示.刚学习c语言的时候一直想做起来,可惜查询好多资料不行.时隔6年多,空闲之余又想起这个问题,便决定一试,虽然c语言已经几乎忘光了, ...

  6. swoole安装

    转自:http://blog.csdn.net/u014207604/article/details/49926207 Windows 下安装 swoole 具体步骤: Swoole,原本不支持在Wi ...

  7. 设计模式入门,观察者模式,c++代码实现

    // test02.cpp : Defines the entry point for the console application.////设计模式第2章 观察者模式#include " ...

  8. SQL Server ->> 关于SQL Server Agent Job执行步骤时的用户上下文(User Context)问题

    这是最近项目相关和自己感兴趣的一个问题:SQL Server Agent Job有几种方法可以以特定用户上下文去执行任务步骤的? 这个事情需要分几种情况来说,因为对于不同类型的任务步骤,SQL Ser ...

  9. leetcode-wildcard matching-ZZ

    http://yucoding.blogspot.com/2013/02/leetcode-question-123-wildcard-matching.html 几个例子: (1) acbdeabd ...

  10. Oracle案例03——RMAN-06091: no channel allocated for maintenance (of an appropriate type)

    同事收到告警磁盘空间不足,说删除归档无法直接在rman中进行操作,让帮看下,具体处理方法如下: 一.错误信息 在rman执行命令 crosscheck archivelog all; delete n ...