AcWing 146. 序列java
原题链接
简单
作者:
henhen敲
,
2020-06-16 14:31:45
,
所有人可见
,
阅读 892
只考虑两组情况就行 然后依次迭代 a是有序的
a1, a2, a3,……, an;
b1, b2, b3,……, bn;
import java.io.*;
import java.util.*;
class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
static class Pair{
int first, second;
public Pair(int first, int second){
this.first = first;
this.second = second;
}
}
static int[] a, b, c;
static int n, m;
static void merge(){
PriorityQueue<Pair> heap = new PriorityQueue<>((o1, o2)->o1.second-o2.second);
for(int i=0; i<n; i++) heap.offer(new Pair(0, a[0]+b[i]));
for(int i=0; i<n; i++){
Pair e = heap.poll();
int f = e.first; int s = e.second;
c[i] = s;
if(f<n-1)heap.offer(new Pair(f+1, s - a[f] + a[f+1]));
}
for(int i=0; i<n; i++) a[i] = c[i];
}
public static void main(String[] args)throws Exception{
int T = nextInt();
while(T--!=0){
m = nextInt(); n = nextInt();
a = new int[n]; b = new int[n]; c = new int[n];
for(int i=0; i<n; i++)a[i] = nextInt();
Arrays.sort(a);
for(int i=0; i<m-1; i++){
for(int j=0; j<n; j++)b[j] = nextInt();
merge();
}
for(int i=0; i<n; i++) out.print(a[i]+" ");
out.println("");
}
out.close();
}
}