Codeforces Round #540 (Div. 3) c题 (codeforce 1118c) Palindromic Matrix

本文介绍了一个来自Codeforces Round #540 (Div. 3)的编程题目C. Palindromic Matrix,讨论如何构建一个回文矩阵,其中详细解释了对于奇数和偶数边长的不同策略,并提供了示例输入和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Palindromic Matrix
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 n2 integers. Put them into a matrix of n rows and n 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 n (1≤n≤20).

The second line contains n2 integers a1,a2,…,an2 (1≤ai≤1000) — the numbers to put into a matrix of n rows and n columns.

Output
If it is possible to put all of the n2 numbers into a matrix of n rows and n 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 n lines with n 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
inputCopy
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1
outputCopy
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1
inputCopy
3
1 1 1 1 1 3 3 3 3
outputCopy
YES
1 3 1
3 1 3
1 3 1
inputCopy
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1
outputCopy
NO
inputCopy
1
10
outputCopy
YES
10
Note
Note that there exist multiple answers for the first two examples.

题意就不说了
n分奇数偶数讨论
若为偶数则每个出现的数必为4的倍数(中心对称)
若为奇数 允许有一个数出现的次数为奇数(处于中行 中列)
允许有(n-1)个数出现次数对4取余为2(处于中间行 或中间列)
上代码

 #include<cstdio>
#include<map>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
int ans[25][25];
int main()
{
    int flag=1;
    map<int,int>::iterator it;
    map<int,int>mp;
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n*n;i++)
    {
        int temp;
        scanf("%d",&temp);
        mp[temp]++;
    }
    if(n%2==0)
    {
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(it->second%4!=0) flag=0;
        }
        if(flag==0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            printf("YES\n");
            it=mp.begin();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i<=n/2&&j<=n/2)
                    {
                        printf("%d ",it->first);
                        ans[i][j]=it->first;
                        it->second-=4;
                        if(it->second==0) it++;
                    }
                    else if(i<=n/2&&j>n/2)
                    {
                        ans[i][j]=ans[i][n+1-j];
                        if(j!=n) printf("%d ",ans[i][j]);
                        if(j==n) printf("%d\n",ans[i][j]);
                    }
                    else
                    {
                        ans[i][j]=ans[n+1-i][j];
                        if(j!=n) printf("%d ",ans[i][j]);
                        if(j==n) printf("%d\n",ans[i][j]);
                    }
                }
            }
        }

    }
    else//weijishu
    {
        int num1=0,num2=0;
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(it->second%4==1) num1++;
            else if(it->second%4==2) num2++;
            else if(it->second%4==3) num1++,num2++;
        }
        if(num1>1||num2>(n-1)) flag=0;
        if(flag==0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            printf("YES\n");
            it=mp.begin();
            for(int i=1;i<=n/2;i++)
            {
                for(int j=1;j<=n/2;j++)
                {
                    while(it->second<4) it++;
                    ans[i][j]=it->first;
                    it->second-=4;
                }
            }
            //shupai
            it=mp.begin();
            for(int i=1;i<=n/2;i++)
            {
                while(it->second<2) it++;
                    ans[i][n/2+1]=it->first;
                    it->second-=2;
            }
            for(int j=1;j<=n/2;j++)
            {
                while(it->second<2) it++;
                    ans[n/2+1][j]=it->first;
                    it->second-=2;
            }
            it=mp.begin();
            while(it->second==0) it++;
            ans[n/2+1][n/2+1]=it->first;
            //printf("%d\n",ans[n/2+1][n/2+1]);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i<=n/2+1&&j<=n/2+1)
                    {
                        if(j==n) printf("%d\n",ans[i][j]);
                        else printf("%d ",ans[i][j]);
                    }
                    else if(i<=n/2+1&&j>n/2+1)
                    {
                        ans[i][j]=ans[i][n+1-j];
                        if(j==n) printf("%d\n",ans[i][j]);
                        else printf("%d ",ans[i][j]);
                    }
                    else
                    {
                        if(j==n) printf("%d\n",ans[n+1-i][j]);
                        else printf("%d ",ans[n+1-i][j]);
                    }
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值