题目链接

B. Morning Jogging

The 2050 volunteers are organizing the "Run! Chase the Rising Sun" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.

There are n+1n+1 checkpoints on the trail. They are numbered by 00, 11, ..., nn. A runner must start at checkpoint 00 and finish at checkpoint nn. No checkpoint is skippable — he must run from checkpoint 00 to checkpoint 11, then from checkpoint 11 to checkpoint 22 and so on. Look at the picture in notes section for clarification.

Between any two adjacent checkpoints, there are mm different paths to choose. For any 1≤i≤n1≤i≤n, to run from checkpoint i−1i−1 to checkpoint ii, a runner can choose exactly one from the mm possible paths. The length of the jj-th path between checkpoint i−1i−1 and ii is bi,jbi,j for any 1≤j≤m1≤j≤m and 1≤i≤n1≤i≤n.

To test the trail, we have mm runners. Each runner must run from the checkpoint 00 to the checkpoint nn once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length lili between checkpoint i−1i−1 and ii (1≤i≤n1≤i≤n), his tiredness is

mini=1nli,mini=1nli,

i. e. the minimum length of the paths he takes.

Please arrange the paths of the mm runners to minimize the sum of tiredness of them.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100001≤t≤10000). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤1001≤n,m≤100).

The ii-th of the next nn lines contains mm integers bi,1bi,1, bi,2bi,2, ..., bi,mbi,m (1≤bi,j≤1091≤bi,j≤109).

It is guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 104104.

Output

For each test case, output nn lines. The jj-th number in the ii-th line should contain the length of the path that runner jj chooses to run from checkpoint i−1i−1 to checkpoint ii. There should be exactly mm integers in the ii-th line and these integers should form a permuatation of bi,1bi,1, ..., bi,mbi,m for all 1≤i≤n1≤i≤n.

If there are multiple answers, print any.

Example

input

Copy

2
2 3
2 3 4
1 3 5
3 2
2 3
4 1
3 5

output

Copy

2 3 4
5 3 1
2 3
4 1
3 5

Note

In the first case, the sum of tiredness is min(2,5)+min(3,3)+min(4,1)=6min(2,5)+min(3,3)+min(4,1)=6.

In the second case, the sum of tiredness is min(2,4,3)+min(3,1,5)=3min(2,4,3)+min(3,1,5)=3.

Analysis:

t组数据,然后给出m(行)n(列),要求每一列的最小值相加,和最小,对m*n个数进行排序之后输出;

Solution:

1-->  STL      rotate(a[p],a[p]+1,a[p]+i);     旋转[ a[p],a[p]+1 )  and [ a[p]+1,a[p]+i ]   /// 下标从0开始

2-->   对全部数据进行从小到大排序b[ ],然后对每一行进行从大到小排序a[ ];n(行)*m(列) 次循环中 找b[ ]中前m个数中(m个数放置在m列)   对应于此时行标的值,作为该列的最小值,这样b[ ]中前m个数就分配在不同行不同列了~   就很神奇。。。

Code one:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int a[110][110];
int main() {int t;cin>>t;while(t--) {int n,m;cin>>n>>m;memset(a,0,sizeof(a));for(int i=0; i<n; i++) {for(int j=0; j<m; j++) {cin>>a[i][j];}sort(a[i],a[i]+m);///每行排序 }for(int i=m; i; i--) {int p=0;for(int j=0; j<n; j++) {if(a[j][0]<a[p][0])///找到第一列最小值  p是行坐标p=j;}rotate(a[p],a[p]+1,a[p]+i);///stl///旋转[a[p],a[p]+1)  and [a[p]+1,a[p]+i]
//          cout<<"++"<<p<<endl;
//          for(int j=0; j<m; j++)
//              cout<<a[p][j]<<" ";
//          cout<<endl;}for(int i=0; i<n; i++) {for(int j=0; j<m; j++) {cout<<a[i][j]<<" ";}cout<<endl;}}return 0;
}

Code two:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int a[110][110];
int c[110];
bool cmp2(int x,int y)
{return x>y;
}
struct node
{int num;///valueint x,y;///row column
}b[10010];
bool cmp(node a,node b)
{return a.num<b.num;
}
int main() {int t;cin>>t;while(t--) {int n,m;cin>>n>>m;memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));for(int i=0; i<n; i++) {for(int j=0; j<m; j++) {cin>>a[i][j];b[i*m+j].num=a[i][j];b[i*m+j].x=i;b[i*m+j].y=j;}sort(a[i],a[i]+m,cmp2);///每行排序   big-->small}sort(b,b+n*m,cmp);///small-->bigfor(int i=0;i<n;i++){for(int j=0;j<m;j++){if(b[j].x==i)cout<<b[j].num<<" ";///find the j column's minn valueelse cout<<a[i][c[i]++]<<" ";///大到小输出 }cout<<endl;}}return 0;
}

CF-B. Morning Jogging相关推荐

  1. 『参考』.net CF组件编程(4)——为自定义组件添加工具箱图标!

    前言: 在前三篇的文章中,和大家一起创建了一个用于TCP连接检测的小组件,如果你记不得了,可以通过以下链接去回顾一下: 『参考』.net CF组件编程(1)--基础之后 『参考』.net CF组件编程 ...

  2. UVA10296 Jogging Trails(中国邮递员问题)(欧拉回路、一般图最大权匹配 / 状压DP)

    整理的算法模板合集: ACM模板 目录 思路 UVA10296 Jogging Trails 题目翻译: 给你n个点,m条无向边,每条边有一定的距离数值,构造成一个连通图.问从任意一点出发,遍历所有的 ...

  3. OC对象 vs CF对象

    2019独角兽企业重金招聘Python工程师标准>>> OC对象 vs CF对象 在ARC场景下,对象所有权没有转换 使用__bridge关键字即可实现CF对象和OC对象之间的自由转 ...

  4. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

  5. 推荐算法——基于协同过滤CF

    https://www.toutiao.com/a6643326861214482957/ 2019-01-06 18:21:09 前边我们已经介绍了推荐算法里的基于内容的推荐算法CB,今天我们来介绍 ...

  6. 索引贴——移动开发(.Net CF 停止更新)

    这是关于本人博客的技术索引贴,希望能方便的让您阅读到相关技术文章--不断更新中.一整理才发现,好多啊,哈哈- 一..Net CF技巧:搜集.转载一些和CF开发相关的辅助文章,比较适合初学者.开发入门者 ...

  7. 解答:CF截图保存在哪

    为什么80%的码农都做不了架构师?>>>    大家玩CF(穿越火线)的时候遇到精彩的画面总希望截图保存下来,然而有些游戏玩家截图后却不知道CF截图保存在哪!这不得不说是个悲剧,但是 ...

  8. CF里面的资源载入问题

    前一段时间已经发现CF在载入资源的时候会怪怪的,但是这一段时间都不曾记起要对这个问题研究一下.最近又发现这个问题了,实在是恼火.俗话说择日不如撞日(粤语),唉,就今天啦.这个问题是在VS2k5里面调试 ...

  9. [CF.Skills]播放嵌入资源的声音文件

    [CF.Skills]播放嵌入资源的声音文件 摘要:本文阐述了在Windows Mobile中如何播放潜入资源的声音文件KeywordsPlaySound, Windows Mobile, Embed ...

  10. CF#190DIV.1

    1 /* 2 CF#190DIV.1-C 3 题意:给你n个结点的树,给这些结点标记字母AB..Z,对于标记相同的结点路径上 4 的结点的标记必须有一个是大于该标记的:问是否可以标记(A是最大标记) ...

最新文章

  1. 项目的简单总结二--可拉伸的头视图
  2. linux脚本控制,linux控制脚本
  3. python *args和**kwargs以及序列解包
  4. 讲师两年升“教授”,成功实现“三连跳”
  5. CentOS7.4搭建FTP服务器(vsftp)
  6. 项目管理笔记(观念)
  7. C# 使用摄像头拍照 支持Win7 64位
  8. 聚聚科技---PHP开发笔试题及答案
  9. axios.js post 后台木有数据
  10. php PHP-FPM进程数的设定
  11. 打开struts-config.xml 报错 解决方法Could not open the editor
  12. Scrapy爬取淘宝网数据的尝试
  13. 《教我兄弟学Android逆向12 编写xpose模块》
  14. Gephi绘制微生物网络图
  15. 【问链-区块链基础知识系列】 第十二课 区块链产业落地现状分析
  16. 海湾gst5000主机消防广播_海湾GST5000消防主机常见问题及解决!
  17. 洛阳中考实验计算机分数,2018洛阳中考科目及分值
  18. TypeError: can‘t compare offset-naive and offset-aware datetimes
  19. 专访许雪松:深入理解嵌入式开发
  20. 自然语言生成技术现状调查:核心任务、应用和评估(3)

热门文章

  1. Java基础:泛型的使用
  2. 成都奔驰加装原厂无钥匙舒适进入 成都名车汇
  3. 计算机培训spss,[计算机软件及应用]SPSS 统计软件培训.ppt
  4. ORACLE学习记录
  5. 非递归实现二叉树的前序、中序、后序遍历
  6. 设计原则之KISS原则和YAGNI原则
  7. 手动查杀浏览器劫持病毒
  8. MATLAB 心形曲线(大赏)
  9. 计算机毕业设计Node.js+Express智慧工地管理系统(源码+程序+lw+远程调试)
  10. 糖尿病最新研究进展(2022年4月)