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

题意:给你\(n\)个数,判断是否能构成一个\(n\)X\(n\)的回文矩阵,若可以,输出\(YES\)和矩阵,否则输出\(NO\).
题解:如果这个矩阵的行/列元素是偶数的话,很好办,所有出现的数一定是\(4\)的倍数,我们直接判断然后模拟输出一下即可.如果是奇数,就要麻烦一点,我们首先用桶存一下所有元素的出现次数,然后直接模拟,首先奇数矩阵的左上右上左下右下一定是对称的,所以我们可以先看左上角的一个小块,模拟一下,每次可以确定\(4\)个位置.之后就是两行中心线了,除了中心,每个位置的元素的对应位置只有一个,所以判断\(2\)即可,再最后是否剩下一个元素给中心即可.
代码:
int n;
int a[N];
map<int,int> mp;
int g[200][200];
bool st[200][200];
int one; int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
n=read();
for(int i=1;i<=n*n;++i){
a[i]=read();
mp[a[i]]++;
} if(n%2==0){
bool flag=true;
for(auto w:mp){
if(w.se%4!=0){
flag=false;
break;
}
}
if(!flag) cout<<"NO"<<endl;
else{
cout<<"YES"<<endl;
for(auto &w:mp){
for(int i=1;i<=n;++i){
bool flag=true;
for(int j=1;j<=n;++j){
if(!st[i][j]){
g[i][j]=w.fi,st[i][j]=true;
g[i][n+1-j]=w.fi,st[i][n+1-j]=true;
g[n+1-i][j]=w.fi,st[n+1-j][j]=true;
g[n+1-i][n+1-j]=w.fi,st[n+1-i][n+1-j]=true;
w.se-=4;
if(w.se==0){
flag=false;
break;
}
}
}
if(!flag) break;
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
cout<<g[i][j]<<" ";
}
cout<<'\n';
}
}
}
else{
int cnt=0;
for(int i=1;i<=n/2;++i){
for(int j=1;j<=n/2;++j){
for(auto &w:mp){
if(w.se>=4){
g[i][j]=w.fi;
g[i][n+1-j]=w.fi;
g[n+1-i][j]=w.fi;
g[n+1-i][n+1-j]=w.fi;
w.se-=4;
cnt++;
break;
}
}
}
}
if(cnt!=(n/2)*(n/2)){
cout<<"NO"<<endl;
return 0;
}
int row=(n/2)+1;
cnt=0;
for(int j=1;j<=n/2;++j){
for(auto &w:mp){
if(w.se>=2){
g[row][j]=w.fi;
g[row][n+1-j]=w.fi;
cnt++;
w.se-=2;
break;
}
}
}
if(cnt!=n/2){
cout<<"NO"<<endl;
return 0;
}
int col=row;
cnt=0;
for(int i=1;i<=n/2;++i){
for(auto &w:mp){
if(w.se>=2){
g[i][col]=w.fi;
g[n+1-i][col]=w.fi;
cnt++;
w.se-=2;
break;
}
}
}
if(cnt!=(n/2)){
cout<<"NO"<<endl;
return 0;
}
for(auto &w:mp){
if(w.se==1){
g[row][col]=w.fi;
cout<<"YES"<<endl;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
cout<<g[i][j]<<" ";
}
cout<<'\n';
}
return 0;
}
}
cout<<"NO"<<endl;
} return 0;
}
Codeforces Round #540 (Div. 3) C. Palindromic Matrix (大模拟)的更多相关文章
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
- 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) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- 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采用的是线性查找,效率低 在 ...
随机推荐
- Go中由WaitGroup引发对内存对齐思考
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 WaitGroup使用大家都会,但是其中是怎么实现的我们 ...
- MySQL的索引优化分析(二)
一.索引优化 1,单表索引优化 建表 CREATE TABLE IF NOT EXISTS article( id INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO ...
- 摆脱 996——GitHub 热点速览 v.21.03
作者:HelloGitHub-小鱼干 Twitter 有位程序员总结了本周的 GitHub 中文程序员的看点:国内程序员日常--考公务员.996.抢茅台.刷算法.整健康码.在本期热点速览里,小鱼干收录 ...
- 攻防世界—pwn—guess_num
题目分析 checksec检查文件保护机制 这个结果看的我满是问号??? \n ida分析程序 是一个猜数字的游戏,需要全部输入正确才能得到flag 根据大佬的wp得出一下内容 先使用srand()进 ...
- 未使用绑定变量对share_pool的影响
oracle SGA中包含数据高速缓冲,重做日志缓冲,以及共享池(share_pool).共享池中包含库高速缓冲(所有的SQL,执行计划等)和数据字典缓冲(对象的定义,权限等). 所以,如果SQL中没 ...
- Poj-P3468题解【线段树】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=3468 题目描述: 给N个数A1, A2 ...
- Vue中组件间通信的方式
Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...
- Hive常用日期格式转换
固定日期转换成时间戳 select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800 select unix_timestamp('2016 ...
- Page (computer memory) Memory segmentation Page table 虚拟地址到物理地址的转换
A page, memory page, or virtual page is a fixed-length contiguous block of virtual memory, described ...
- RabbitMQ入门看这一篇就够了
一文搞懂 RabbitMQ 的重要概念以及安装 一 RabbitMQ 介绍 这部分参考了 <RabbitMQ实战指南>这本书的第 1 章和第 2 章. 1.1 RabbitMQ 简介 Ra ...