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 ...
随机推荐
-
CENTOS GUI
http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 How t ...
-
mysql字段的适当冗余有利于提高查询速度
CREATE TABLE `comment` ( `c_id` int(11) NOT NULL auto_increment COMMENT '评论ID', `u_id` int(11) NOT ...
-
JSON ,JSONArray和JSONObject
和 XML 一样,JSON 也是基于纯文本的数据格式.由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,可以用 JSON 传输一个简单的 String,Num ...
-
BZOJ_1002_[FJOI2007]_轮状病毒_(递推+高精)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1002 )*&*(^&*^&*^**()*) 1002: [FJOI20 ...
-
夏宇闻教授谈FPGA工程师的入门学习
1. 必须清楚自己究竟适合不适合做工程师. 看看自己的性格特点,是不是特别安静,又耐得住寂寞.因为FPGA工程师是一个辛苦的工作,不但要通过不断学习研究提升自己的设计水平,还要经常性的熬夜加班敲写代码 ...
-
iframe自适应高度代码
var adjustIframe = function (id) { var iframe = document.getElementById(id) var idoc = iframe.conten ...
-
数据库元数据MetaData
本篇介绍数据库方面的元数据(MetaData)的有关知识.元数据在建立框架和架构方面是特别重要的知识,再下一篇我们仿造开源数据库工具类DbUtils就要使用数据库的元数据来创建自定义JDBC框架. 在 ...
-
1523. K-inversions URAL 求k逆序对,,,,DP加树状数组
1523. K-inversions Time limit: 1.0 secondMemory limit: 64 MB Consider a permutation a1, a2, …, an (a ...
-
AndroidStudio制作底部导航栏以及用Fragment实现切换功能
前言 大家好,给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢 学习目标 AndroidStudio制作底部导航栏以及用Fragment实现切换 ...
-
js获取select标签选中的值及文本
原生js方式: var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // ...