CF-B. Morning Jogging

题目链接

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 )  anda[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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Address extends JFrame { private String name; private String phone_Number; private String address; private String id; private String habit; private String[] btnTexts = { "Information 1", "Information 2", "Information 3" }; private String dialogMsg = "Your Name is:%s Your Phone_Number is:%s Your address is:%s Your id is:%s Your habit is:%s"; public Address() { setTitle("Address"); setSize(700, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); add(new JButton("Choose Your Information"), BorderLayout.NORTH); add(new JButton("<<="), BorderLayout.WEST); JPanel panel = new JPanel(); panel.setBackground(Color.RED); panel.setLayout(new GridLayout(3, 1, 10, 10)); add(panel, BorderLayout.CENTER); for (String text : btnTexts) { JButton btn = new JButton(text); panel.add(btn); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String msg = String.format(dialogMsg, name, phone_Number, address, id, habit); JOptionPane.showMessageDialog(btn, msg); } }); } } public static void main(String[] args) { Address a = new Address(); a.setVisible(true); } } 将这段代码展现出的结果通过修改代码来达到美观化和高级化的目的
最新发布
05-18
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值