C. Mex in the Grid

题目链接:https://codeforces.com/contest/2102/problem/C

题意:

一共有0~n^2-1个数要填到nxn的矩阵里,并且要求子矩阵的mex之和最大

思路:

构造题

猜了一个蛇形矩阵竟然过了你敢信?

大概是把0,1,2,3...这些比较小的数填在矩阵中心的位置能提高最多的子矩阵的下限值吧

int g[1050][1050];
void solve(){
    int n;cin>>n;
    int now=n*n-1;
    int left=1,right=n;
    int top=1,bottom=n;
    int a=left,b=top;
    while(left<=right&&top<=bottom){
        for(int i=left;i<=right;i++){
            g[top][i]=now--;
        }
        top++;if(top>bottom)break;
        for(int i=top;i<=bottom;i++){
            g[i][right]=now--;
        }
        right--;if(right<left)break;
        for(int i=right;i>=left;i--){
            g[bottom][i]=now--;
        }
        bottom--;if(top>bottom)break;
        for(int i=bottom;i>=top;i--){
            g[i][left]=now--;
        }
        left++;if(right<left)break;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<g[i][j]<<' ';
        }
        cout<<endl;
    }
}
posted @ 2025-05-12 16:39  Marinaco  阅读(128)  评论(0)    收藏  举报
//雪花飘落效果