codeforces625C

K-special Tables

 CodeForces - 625C 

人们经常做一些疯狂的事来凸显自己。有的人跳舞,有的人撩妹,有的人立志成为顶级程序猿(例如某peng),还有的人喜欢收集有趣的数学对象

Alice对上面的东西都很喜欢。现在她想要得到一张k-特殊表。一个大小为n*n的特殊表需要满足一下三个条件:

  • 1~n2之间的所有数都在表中出现恰好一次。
  • 每排内部的数递增排列。
  • 第k列的数之和尽可能的大

你的目标是帮助Alice找到至少一个n*n大小的k-特殊表。每排每列由1~n进行标号,每排按从上到下的顺序,每列按从左到右的顺序。

Input

第一行输入包含两个数:n和k(1 ≤ n ≤ 500, 1 ≤ k ≤ n)

Output首先输出第k列数字之和。

接下来输出的n行用于描述你所构造的表:第一行依次输出表中第一排的n个数,第二行依次输出表中第二排的n个数,以此类推。

如果存在多种答案,输出其中任意一种

Sample Input

Input
4 1
Output
28
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
 
===================
Input
5 3
Output
85
5 6 17 18 19
9 10 23 24 25
7 8 20 21 22
3 4 14 15 16
1 2 11 12 13
 
sol:较基础的构造,显然对于每行,第k个到第n个元素应该是最大的
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('
')
const int N=505;
int n,m,a[N][N];
int main()
{
    int i,j,Now,Sum=0;
    R(n); R(m);
    Now=n*n;
    for(i=1;i<=n;i++)
    {
        for(j=n;j>m;j--) a[i][j]=Now--;
        Sum+=Now; a[i][m]=Now--;
    }
    for(i=1;i<=n;i++)
    {
        for(j=m-1;j>=1;j--) a[i][j]=Now--;
    }
    Wl(Sum);
    for(i=1;i<=n;i++,puts(""))
    {
        for(j=1;j<=n;j++) W(a[i][j]);
    }
    return 0;
}
/*
input
5 3
output
85
*/
View Code
原文地址:https://www.cnblogs.com/gaojunonly1/p/10617620.html