Description
Input
Output
Sample Input
1 2 3 1 2 3 2 2 3
Sample Output
3 3 4
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int n, m;
int cnt , i, j, k;
int str1[2010], str2[2010];
priority_queue<int> str;
cin >> cnt;
while(cnt--){
cin >> m >> n;
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
for(i = 0; i < n; i ++)
scanf("%d", &str1[i]);
sort(str1, str1 + n);
for(i = 1; i < m; i++){
for(j = 0; j < n; j++)
scanf("%d", &str2[j]);
sort(str2, str2 + n);
for(j = 0; j < n; j ++)
str.push(str2[0] + str1[j]);
for(j = 1; j < n; j ++){
for(k = 0; k < n; k ++){
if(str2[j] + str1[k] > str.top())
break;
str.pop(); //只要新的比堆里最大的小,就把堆里最大的扔掉,把新的放进堆
str.push(str2[j] + str1[k]);
}
}
for(j = 0; j < n; j ++){
str1[n - j -1] = str.top(); //更新str1
str.pop();//最后堆为空
}
}
for(i = 0; i < n; i ++)
{
if(i != 0)
cout<<" ";
cout<<str1[i];
}
cout<<endl;
}
return 0;
}
5959



被折叠的 条评论
为什么被折叠?



