思路:
- 首先计算2路时的前n
- 然后根据第i路的前n,加上第i+1路的数据,作为新的2路前n进行计算
2路前n的计算方法:
a 排序后,ax + by 的前 i 必然位于下面的候选项里:(b非排序,如果排序则最多到bi)
a0 + b0, a1 + b0, a2 + b0, …
a0 + b1, a1 + b1, a2 + b1, …
…
a0 + bi, a1 + bi, a2 + bi, …
from heapq import *
N = 2010
a, b, c = [[0] * N for _ in range(3)]
def merge():
global a, b, c
q = []
for i in range(n):
heappush(q, (b[i] + a[0], 0))
for i in range(n):
val, ai = heappop(q)
c[i] = val
if ai < n - 1:
heappush(q, (val - a[ai] + a[ai + 1], ai + 1))
a = list(c)
t = int(input())
while t:
t -= 1
m, n = map(int, input().split())
a = sorted(map(int, input().split()))
for i in range(m - 1):
b = list(map(int, input().split()))
merge()
for i in range(n):
print(a[i], end=' ')
print('')