Sequence《优先队列》

时间:2023-03-09 16:54:20
Sequence《优先队列》

Description

Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?

Input

The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.

Output

For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.

Sample Input

1
2 3
1 2 3
2 2 3

Sample Output

3 3 4

这个题的意思不是很难理解,关键是思想,一点点算肯定超时了;
 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
int m,n,t,a[],b[],i,j;
priority_queue<int,vector<int>,less<int> >que;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&m,&n);
m--;
for(i=; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
while(m--)
{
for(i=; i<n; i++)
{
scanf("%d",&b[i]);
que.push(a[]+b[i]);//先进去N个
}
sort(b,b+n);//这个应该会用了
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
if(a[i]+b[j]>que.top())//因为有sort排序,所以,后面只会更大,可以break;
break;
que.pop();
que.push(a[i]+b[j]);
}
}
for(i=n-; i>-; i--)
{
a[i]=que.top();
que.pop();
}
}
printf("%d",a[]);
for(i=; i<n; i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}

Hint

Huge input,scanf is recommended.