AcWing 146. 序列——Java代码版
原题链接
简单
作者:
三玖天下第一
,
2021-03-06 11:14:26
,
所有人可见
,
阅读 366
import java.io.*;
import java.util.*;
public class 序列 {
static PriorityQueue<Pair> queue = new PriorityQueue<>(2002, Comparator.comparingInt(o -> o.first));
static int[] a = new int[2002], b = new int[2002], c = new int[2002];
static int n, m;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] temp;
int t = Integer.parseInt(reader.readLine().trim());
while(t-- > 0){
temp = reader.readLine().split(" ");
m = Integer.parseInt(temp[0]);
n = Integer.parseInt(temp[1]);
temp = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(temp[i]);
}
Arrays.sort(a, 0, n);
for (int i = 0; i < m - 1; i++) {
temp = reader.readLine().split(" ");
for (int j = 0; j < n; j++) {
b[j] = Integer.parseInt(temp[j]);
}
merge();
}
for (int i = 0; i < n; i++) {
writer.write(a[i]+" ");
}
writer.write("\n");
writer.flush();
}
}
static void merge(){
queue.clear();
for (int i = 0; i < n; i++) {
queue.add(new Pair(b[i]+a[0], 0));
}
for (int i = 0; i < n; i++) {
Pair p = queue.poll();
c[i] = p.first;
queue.add(new Pair(p.first-a[p.second]+a[p.second+1], p.second+1));
}
System.arraycopy(c, 0, a, 0, n);
}
static class Pair{
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
}