Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C
C. 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.
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
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 【暴力】的更多相关文章
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix (大模拟)
题意:给你\(n\)个数,判断是否能构成一个\(n\)X\(n\)的回文矩阵,若可以,输出\(YES\)和矩阵,否则输出\(NO\). 题解:如果这个矩阵的行/列元素是偶数的话,很好办,所有出现的数一 ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
- 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> ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- 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 ...
- Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)
https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...
- Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)
https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...
随机推荐
- js验证港澳居民通行证号码是否合规
需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于验证港澳居民通行证号码的代码,总结一下. 代码: function checkHKMacao(code){ var tip = ...
- 兼容IE和Firefox获得keyBoardEvent对象
<input type="text" name="words" id="search_txt" class="seachIp ...
- FW:Software Testing
Software Testing Testing with a Purpose Software testing is performed to verify that the completed s ...
- [android] 天气app布局练习(三)
主要练习LinearLayout和layout_weight属性 <RelativeLayout xmlns:android="http://schemas.android.com/a ...
- css之表格,表单
一.表格 1.定义 表格由<table>标签来定义.每个表格均有若干行(由tr标签定义),每行被分割为若个单元格(由td标签定义).字母td指表格数据(table data),即数据单元格 ...
- 用iframe踩的坑
1.无法监控iframe加载成功与否 经测试,火狐及chorme都不支持onerror事件,而且,不管iframe加载是否成功,都会触发onload事件. 1)通过postmessage消息提示是否加 ...
- asp.net怎么让某一页的 requestEncoding设置成utf-8
web.config里是这样的 <globalization requestEncoding="gb2312" responseEncoding="gb2312&q ...
- Python爬虫教程-30-Scrapy 爬虫框架介绍
从本篇开始学习 Scrapy 爬虫框架 Python爬虫教程-30-Scrapy 爬虫框架介绍 框架:框架就是对于相同的相似的部分,代码做到不出错,而我们就可以将注意力放到我们自己的部分了 常见爬虫框 ...
- Javascript之深入理解闭包
闭包算是js里面比较不容易理解的点,尤其是对于没有编程基础的人来说. 其实闭包要注意的就那么几条,如果你都明白了那么征服它并不是什么难事儿.下面就让我们来谈一谈闭包的一些基本原理. 闭包的概念 一个闭 ...
- idea打jar包
昨天碰到个问题:使用idea打成jar包,但是在测试环境一直报错.参考: http://blog.csdn.net/aotian16/article/details/52198378 之后发现原来的j ...