【77.78%】【codeforces 625C】K-special Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.
Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is called k-special if the following three conditions are satisfied:
• every integer from 1 to n2 appears in the table exactly once;
• in each row numbers are situated in increasing order;
• the sum of numbers in the k-th column is maximum possible.
Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.
Output
First print the sum of the integers in the k-th column of the required table.
Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.
If there are multiple suitable table, you are allowed to print any.
Examples
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
【题目链接】: http://codeforces.com/contest/625/problem/C
【题解】
贪心.
尽量让小的数字先在前k-1列出现.剩下的数按顺序每一行从左往右安排就可以了.;
看样例也能看出来了.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 500+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n,k;
int a[MAXN][MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
int now = 0;
rep1(i,1,n)
rep1(j,1,k-1)
a[i][j] = ++now;
rep1(i,1,n)
rep1(j,k,n)
a[i][j] = ++now;
LL temp = 0;
rep1(i,1,n)
temp += a[i][k];
cout << temp << endl;
rep1(i,1,n)
rep1(j,1,n)
{
printf("%d",a[i][j]);
if (j==n)
puts("");
else
putchar(' ');
}
return 0;
}
【77.78%】【codeforces 625C】K-special Tables的更多相关文章
- 【CodeForces 625C】K-special Tables
题意 把1到n*n填在n*n的格子里.要求每一行都是递增的,使第k列的和最大. 分析 第k列前的格子1 2 .. 按要求填到满格,然后第k列及后面的格子,都从左到右填递增1的数. 第k列的和再加起来, ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【链表】【模拟】Codeforces 706E Working routine
题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...
随机推荐
- 洛谷 P1757 通天之分组背包
P1757 通天之分组背包 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品 ...
- 负载均衡器&http正向代理
透明的负载均衡器&http正向代理 * master-workers架构,http正向代理由独立的dns请求以及缓冲进程 * 使用epoll(ET)模式,採用全异步方式(双缓存,实现双向同一 ...
- 内网使用 IPV6 之Teredo篇
这篇转载自 http://bbs.pcbeta.com/viewthread-1580771-1-1.html 上IPv6站点之Teredo篇http://bbs.pcbeta.com/viewthr ...
- 68.qq号索引结构体写入内存,并实现快速排序
//两个步骤,第一步读取文件,并且初始化索引结构体,把初始化的索引结构体写入到文件,第二步,读取这个文件到索引结构体 //并对这个结构体进行快速排序,得到顺序的索引,再写入文件 #define _CR ...
- Java学习笔记六 常用API对象二
1.基本数据类型对象包装类:见下图 public class Test { public static void main(String[] args){ Demo(); toStringDemo() ...
- golang sync.Cond
package main import ( "fmt" "sync" "time" ) func main() { wait := sync ...
- BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机/后缀自动机)
Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串 ...
- 手动挂接NFS
环境: 单板:s3c2440 内核:Linux-2.6.22.6 U-boot1.16 初始根文件系统Yaffs2 前提条件 1. 开发板上要烧写好文件系统 2. 能正常开机进入Linux系统 3. ...
- 【2017"百度之星"程序设计大赛 - 初赛(B)】Chess
[链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=776&pid=1001 [题意] 在这里写题意 [题 ...
- code-代码平台服务器路径
下面记录的是各个平台的服务器路径(va使用) ("repo init -u ssh://vanzo/platform_89/manifest.git") ("repo i ...