zoukankan      html  css  js  c++  java
  • Codeforces Round #540 (Div. 3) c题 (codeforce 1118c) Palindromic Matrix

    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
    ");
                return 0;
            }
            else
            {
                printf("YES
    ");
                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
    ",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
    ",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
    ");
                return 0;
            }
            else
            {
                printf("YES
    ");
                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
    ",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
    ",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
    ",ans[i][j]);
                            else printf("%d ",ans[i][j]);
                        }
                        else
                        {
                            if(j==n) printf("%d
    ",ans[n+1-i][j]);
                            else printf("%d ",ans[n+1-i][j]);
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    python_函数_文件
    Day_2_Python_str_list_dict的使用
    Day_1_Python_循环和格式化
    influxdb2.0版本部署+自启
    格式化Java内存工具JOL输出
    卷心菜的屯币日记
    influxDB时序数据库2.0FLUX查询语法使用记录
    两种转换2021-01-01T00:00:00Z为2021-01-01 00:00:00时间格式的方式(UTC时间转为yyyy-MM-dd HH:mm:ss)
    ThreadLocal的用处
    CentOS7使用ISO镜像文件作为离线Yum源
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852341.html
Copyright © 2011-2022 走看看