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;///value
int 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-->big
for(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 value
else cout<<a[i][c[i]++]<<" ";///大到小输出
}
cout<<endl;
}
}
return 0;
}