C. K-special Tables【构造】水

题意:给一个n和k的,要求第k列的和最大,n*n的矩阵,要求1到n*n所有数都出现,且每一行从左到右要求为递增

题解:第k列之后一定只增加一,按照这个构造即可

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007

int n,m;
int a[550][550];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t,C = 1;
    //scanf("%d",&t);
    while(scanf("%d%d",&n,&m) != EOF)
    {
        int pos1 = 1,pos2 = (m-1)*n+1;
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                if(j == m)
                    ans += pos2;
                if(j >= m)
                    a[i][j] = pos2++;
                else
                    a[i][j] = pos1++;
            }
        printf("%d\n",ans);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
                printf("%d ",a[i][j]);
            printf("\n");
        }
    }
    return 0;
}


阅读更多
版权声明:随意转载,转载声明出处~ https://blog.csdn.net/Ezereal/article/details/52353418
上一篇codeforces 624d 623b Array GCD ★ ★
下一篇构造 D - Finals in arithmetic ★ ★
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭