HDOJ 1280 前m大的数

前m大的数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7048    Accepted Submission(s): 2508


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
 
Author
Gardon
 
Source
 
Recommend
lcy
 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 int gank[10001];
 8 int a[3022];
 9 
10 int main()
11 {
12     int n,m;
13     while(scanf("%d%d",&n,&m)!=EOF)
14     {
15         memset(gank,0,sizeof(gank));
16         for(int i=0;i<n;i++)
17             scanf("%d",&a[i]);
18         for(int i=n-1;i>=0;i--)
19             for(int j=0;j<i;j++)
20         {
21             gank[a[i]+a[j]]++;
22         }
23 
24         int first=1;
25         for(int i=10000;i>=0&&m>0;)
26         {
27             if(gank[i]==0) i--;
28             else
29             {
30                 if(first)
31                 {
32                     printf("%d",i);
33                     first=0;
34                 }
35                 else
36                 printf(" %d",i);
37                 gank[i]--;
38                 m--;
39             }
40         }
41 
42         putchar(10);
43     }
44 
45     return 0;
46 }
原文地址:https://www.cnblogs.com/CKboss/p/3098606.html