hoj 3272 桶排序

来源:互联网 发布:时时彩源码境外服务器 编辑:程序博客网 时间:2024/06/05 09:42

Problem Description

有n(n <= 3000)个不大于5000的正整数,对它们两两相加得到N = n * (n - 1) / 2个数,求这N个数中最大的m(1 <= m <= min(1000, N))个数。


Input

多组数据。每组数据的第一行是两个整数n和m,第二行是n个整数。

Output

对于每组输入的数据,输出一行表示答案。数字与数字之间以空格分离。输出应该按照从大到小的顺序排列。

Sample Input

4 4

1 2 3 4

4 5

5 3 6 4


Sample Output

7 6 5 5

11 10 9 9 8

#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 10000int arr[maxn];int brr[maxn];int main(){    int i,j;    int m,n;    while(scanf("%d%d",&n,&m)!=EOF && n != 0)    {        memset(brr,0,sizeof(brr));        int count = 10000;        for(i = 0;i < n; i++) scanf("%d",&arr[i]);        for(i = 0;i < n; i++)        {            for(j = i + 1;j < n; j++)            {                brr[arr[i]+arr[j]] += 1;            }        }        if(m > 1000) m = 1000;        while(m)        {            while(brr[--count] == 0);            for(i = 0;i < brr[count];i++)            {                printf("%d ",count);                m--;                if(m == 0) break;            }        }        printf("\n");    }    return 0;}