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 ...
随机推荐
- [css]我要用css画幅画(三)
接着之前的[css]我要用css画幅画(二), 今天,我画了一个小人,他的名字暂时叫作小明. 以下只列出本次修改增加的内容 html如下: <div class="human left ...
- 漫谈C语言结构体struct、公用体union空间占用
先用代码说话: #include<stdio.h> union union_data0{ int a ;//本身占用4个字节 char b ;//本身占用1个字节 int c ; }; u ...
- 将String转化成Stream,将Stream转换成String
using System;using System.IO;using System.Text;namespace CSharpConvertString2Stream{ class Progr ...
- SqlServer数据冗余的问题和解决
1问题: 1>造成了存储空间的浪费. 2>更新异常.删除异常. 所以一般情况不允许在表中出现数据冗余. 2怎么解决? 把原来表中的数据拆分成多个表来存储. 当把表中的信息拆分成多个表来存储 ...
- javascript - 享元模式
享元模式笔记 运用共享技术有效的支持大量的细粒度对象,避免对象间拥有相同内容造成多余的开销 享元模式主要还是对其数据.方法共享分离,它将数据和方法分成内部数据.内部方法和外部数据.外部方法. 内 ...
- WPF Telerik TreeListView样式设计
Telerik控件 TreeListView 修改其中样式 1.添加TreeListView控件 <telerik:RadTreeView x:Name="ObjecTreeView& ...
- android edittext属性说明
将EditText内容转换为字符串: EditText.getText().toString() <EditText android:id="@+id/edt_month" ...
- 4412开发板学习笔记-NFS服务器的搭建
转自iTOP-4412讨论群: http://www.topeetboard.com 先来介绍一下NFS: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件 ...
- Asp.Net Core 简单的使用加密的Cookie保存用户状态
在以前的Asp.Net中可以用 FormsAuthentication 类的一系列方法来使用加密的Cookie存储用户身份,使用简单,可控性强.在Asp.Net Core中是否也可以的?答案是当然的. ...
- java设计模式之原型模式
原型模式概念 该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.java中复制通过clone()实现的.clone中涉及深.浅复制.深.浅复制的概念如下: ⑴浅复制 ...