• 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]);
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    Mysql(三) Mysq慢查询日志
    Mysql(二) Mysql错误日志
    Mysql(一) Mysql二进制日志
    HA(二)Heartbeat实现LVS集群DR模式下Director高可用
    HA(一)高可用集群原理
    LVS(五)LVS集群RealServer高可用健康监测
    LVS(四)LVS集群DR模式
    LVS(三)LVS集群NAT模式
    LVS(二)LVS集群中实现的三种负载均衡技术
    Cocos2d-JS实现的打飞机
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852341.html
Copyright © 2020-2023  润新知