Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)
Description
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.
Sample Input
4 1
5 3
Sample Output
281 2 3 45 6 7 89 10 11 1213 14 15 16
855 6 17 18 199 10 23 24 257 8 20 21 223 4 14 15 161 2 11 12 13
思路
题意:
输出一个的n*n方阵,使得其每行为上升序列,方阵中1~n2的数字每个只能出现一次,方阵的第k列的和要最大
题解:
从样例出发,5*5的方阵中,使得第三列的和最大,倒过来看,为了满足上升序列,最后一行的倒三个数最大只能为23,倒二行的倒三个数只能为20,依此类推。剩下的未填数的格子,填入符合要求的数字即可
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
int x = 1,y = n*n - (n - k + 1) * n + 1;
printf("%d\n",y*n+(n-k+1)*n*(n-1)/2);
for (int i = 0;i < n;i++)
{
for (int u = 0;u < k - 1;u++) u?printf(" %d",x++):printf("%d",x++);
for (int u = k - 1;u < n;u++) u?printf(" %d",y++):printf("%d",y++);
printf("\n");
}
return 0;
}
Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)的更多相关文章
- Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)
传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...
- Codeforces Round #298 (Div. 2) A、B、C题
题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心
D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...
- Codeforces Round #342 (Div. 2) C. K-special Tables 构造
C. K-special Tables 题目连接: http://www.codeforces.com/contest/625/problem/C Description People do many ...
- Codeforces Round #342 (Div. 2)-B. War of the Corporations
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #342 (Div. 2)
贪心 A - Guest From the Past 先买塑料和先买玻璃两者取最大值 #include <bits/stdc++.h> typedef long long ll; int ...
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...
- Codeforces Round #342 (Div. 2) A - Guest From the Past 数学
A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...
随机推荐
- yii2实战教程之第一个Yii程序
之前考虑过要不要砍掉该章节,直接上手教你搭建简单的博客系统.出于实战基础加之自C语言的书籍出版以来,几乎所有的编程书籍都讲述了一个Hello World的例子作为开始.虽然我们仅仅是学习Yii2,但是 ...
- MVC3.0----整理之一
1.ViewBag对象是动态解析对象 @{ ViewBag.Title = "Insert"; } 不仅可以把单个的对象绑定到 ViewBag上,还可以直接绑定List ViewB ...
- Reporting Services 错误案例一则
遇到一个有意思的Reporting Services报表的案例,在2015-01-30号的凌晨20分左右的时候,有人发现Reporting Services的速度非常慢,而且最后有抛出异常,当时不知道 ...
- SQL Server删除distribution数据库
在数据库服务器删除复制(发布订阅)后,如何删除掉数据库distribution呢?如果你通过SSMS工具去删除数据库distribution,你会发现根本没有删除选项. 下面介绍一下删除distrib ...
- .NET应用架构设计—面向查询服务的参数化查询设计(分解业务点,单独配置各自的数据查询契约)
阅读目录: 1.背景介绍 2.对业务功能点进行逻辑划分(如:A.B.C分别三个业务点) 2.1.配置映射关系,对业务点配置查询契约(构造VS插件方便生成查询契约) 2.2.将配置好的映射策略文件放在调 ...
- mysql-7 数据检索(5)
联结(join) SQL最强大的功能之一就是能在数据检索查询的执行中联结(join)表.联结就是利用SQL的SELECT能执行的最重要的操作. 例子:此例子包含两个表,一个表是vendors , ve ...
- Vim插件管理
一.简介 二.管理器 1)Vundle https://github.com/gmarik/vundle.git 2)vim-plug https://github.com/junegunn/vim- ...
- 64位的Ubuntu系统上使用汇编nasm和C语言
64位的Ubuntu系统上使用汇编nasm和C语言 $ nasm -f elf foo.asm -o foo.o$ gcc -c bar.c -o bar.o$ ld -s foo.o bar.o ...
- 【转】SVN提交一般原则
写在前面: 最近, 跳槽了. 来到了一家感觉QIAN景不错的创业公司. 由于是开始, 规模比较小, SVN的管理工作, 也有我来承担. 今天发生了一些相当不愉快的事情, 就是因为一个同事, ①签入代码 ...
- TFS 升级错误一则 TF400654
升级后报 [错误] TF400654: 无法配置规划工具.以下元素包含错误: BugWorkItems/BugWorkItems.TF400506: 此元素将定义表示 Bug 或缺陷的工作项的状态.每 ...