Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门: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:

  1 #include <bits/stdc++.h>
  2 #define INF 0x3f3f3f3f
  3 #define LL long long
  4 using namespace std;
  5 const int MAXN = 1e3+10;
  6 int sum[MAXN];
  7 int ans[50][50];
  8 int n, num;
  9
 10 int main()
 11 {
 12     scanf("%d", &n);
 13     int N = n*n, x;
 14     for(int i = 1; i <= N; i++){
 15         scanf("%d", &x);
 16         sum[x]++;
 17     }
 18     int flag = true;
 19     int mid = (n+1)/2;
 20     int num = 1;
 21     if(n%2 == 0){
 22         for(int i = 1; i <= mid; i++){
 23             for(int j = 1; j <= mid; j++){
 24                 for(; num <= 1000; num++){
 25                     if(sum[num]){
 26                         if(sum[num]%4){
 27                             flag = false;
 28                             break;
 29                         }
 30                         ans[i][j] = ans[i][n-j+1] = ans[n-i+1][j] = ans[n-i+1][n-j+1] = num;
 31                         sum[num]-=4;
 32                         break;
 33                     }
 34                 }
 35                 if(num > 1000) flag = false;
 36                 if(!flag) break;
 37             }
 38             if(!flag) break;
 39         }
 40     }
 41     else{
 42         flag = true;
 43         num = 1;
 44         for(; num <= 1000; num++){
 45             if(sum[num]%2){
 46                 ans[mid][mid] = num;
 47                 sum[num]--;
 48                 break;
 49             }
 50         }
 51         if(num > 1000){
 52             flag = false;
 53             //puts("zjy");
 54         }
 55         else{
 56             num = 1;
 57             for(int i = 1; i < mid; i++){
 58                 for(int j = 1; j < mid; j++){
 59                     for(; num <= 1000; num++){
 60                         if(sum[num] == 0) continue;
 61                         if(sum[num]/4 < 1) continue;
 62                         ans[i][j] = ans[i][n-j+1] = ans[n-i+1][j] = ans[n-i+1][n-j+1] = num;
 63                         sum[num]-=4;
 64                         break;
 65                     }
 66                     if(num > 1000) flag = false;
 67                     if(!flag) break;
 68                 }
 69                 if(!flag) break;
 70             }
 71
 72             num = 1;
 73             for(int i = 1; i < mid; i++){
 74                 for(; num <= 1000; num++){
 75                     if(sum[num]/2 >= 1){
 76                         ans[mid][i] = ans[mid][n-i+1] = num;
 77                         sum[num]-=2;
 78                         break;
 79                     }
 80                 }
 81                 if(num > 1000){flag = false; break;}
 82             }
 83
 84             num = 1;
 85             for(int i = 1; i < mid; i++){
 86                 for(; num <= 1000; num++){
 87                     if(sum[num]/2 >= 1){
 88                         ans[i][mid] = ans[n-i+1][mid] = num;
 89                         sum[num]-=2;
 90                         break;
 91                     }
 92                 }
 93                 if(num > 1000){flag = false; break;}
 94             }
 95         }
 96     }
 97
 98     if(!flag) puts("NO");
 99     else{
100         puts("YES");
101         for(int i = 1; i <= n; i++){
102             for(int j = 1; j <= n; j++)
103                 printf("%d ", ans[i][j]);
104             puts("");
105         }
106     }
107
108     return 0;
109 }

原文地址:https://www.cnblogs.com/ymzjj/p/10404382.html

时间: 2024-09-30 11:16:35

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】的相关文章

Codeforces Round #427 (Div. 2) D. Palindromic characteristics(Manacher求回文串)

题目链接:Codeforces Round #427 (Div. 2) D. Palindromic characteristics 题意: 给你一个串,定义k-th回文串,让你求每个k-th的数量. 题解: manacher处理好后做一下dp就行了. 当然也可以直接dp不用manacher. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 cons

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

Codeforces Round #540 (Div. 3) 部分题解

Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选一些题来写吧. B. Tanya and Candies 题意: 在n个数中任意删去一个,如果这个数被删去后,剩余数的奇数和以及偶数和相等,那么就定义这个数为"好数".现在问这n个数中有多少个“好数”. 题解: 预处理出奇数前缀和.偶数前缀和,删去一个数后所有的奇数位置和 就为前面的奇数和

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

Codeforces Round #427 (Div. 2) D. Palindromic characteristics

D. Palindromic characteristics Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if

Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an undirected tree of nn vertices. Some vert

Codeforces Round #540 (Div. 3)

A.题目意思理解就过了 B.读了题目没有写,但是依稀感觉是前缀和,不过写残了.题解代码非常简洁. 维护前缀.后缀奇数和,维护前缀.后缀偶数和. 题解把遍历过的数都减去了,也就维护了后缀和. 前缀过一个加一个. 1 #include<bits/stdc++.h> 2 typedef long long ll; 3 int a[200010]; 4 5 int main() { 6 int n,Osum=0,Esum=0,Oper=0,Eper=0; 7 scanf("%d",

Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                              D. Once Again... You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of