您的位置:首页 > 产品设计 > UI/UE

Sequence用堆排序

2014-05-26 21:05 10 查看
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"algorithm"
#include"ctime"
#include"cstdio"
#include"cctype"
using namespace std;
#define maxx 2008
int a[maxx],b[maxx],sum[maxx];
int main()
{
int i,j,k,t,n,m,temp;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<m;i++)
{
sort(a,a+n);
for(j=0;j<n;j++)
scanf("%d",&b[j]);
for(j=0;j<n;j++)
sum[j]=a[j]+b[0];
make_heap(sum,sum+n);
for(j=1;j<n;j++)
for(k=0;k<n;k++)
{
temp=b[j]+a[k];
if(temp>=sum[0])
break;
pop_heap(sum,sum+n);
sum[n-1]=temp;
push_heap(sum,sum+n);
}
for(j=0;j<n;j++)
a[j]=sum[j];
}
sort(a,a+n);
printf("%d",a[0]);
for(j=1;j<n;j++)
printf(" %d",a[j]);
printf("\n");
printf("time :%d\n",clock());
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: