poj——1280——前m大的数

来源:互联网 发布:男士商务单肩包 知乎 编辑:程序博客网 时间:2024/06/06 05:15
Problem Description
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。 
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
 


Input
输入可能包含多组数据,其中每组数据包括两行: 
第一行两个数N和M, 
第二行N个数,表示该序列。


 


Output
对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
 


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 <cstdio>#include <cstring>int a[3005],cnt[10005];bool hash[10005];int main (){int m,n;while (~scanf("%d%d",&n,&m)){int i;for (i=0;i<n;i++)scanf("%d",&a[i]);memset (hash,0,sizeof(hash));memset (cnt,0,sizeof(cnt));for (i=0;i<n;i++)for (int j=i+1;j<n;j++){   hash[a[i]+a[j]]=true;   cnt[a[i]+a[j]]++;}for (i=10000;m>=1;i--)if (hash[i]==true)                while (cnt[i]-- && m)printf(m-->1?"%d ":"%d\n",i);}return 0;}


 


0 0
原创粉丝点击