【hdu 1280 前m大的数】

前m大的数

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


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
 
====================================================================
据说可以用hash实现。我用了hash的思路搞定的。。大概就是这个意思吧。
====================================================================
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <fstream>
 4 #include <cstring>
 5 
 6 namespace ism
 7 {
 8     using namespace std;
 9     ifstream cin ("in.dat", ios::in);
10 };
11 
12 using std::cin;
13 using std::cout;
14 using std::endl;
15 #define MAXN 10010
16 
17 int iNumArr[MAXN / 2];
18 int iSumArr[MAXN];
19 int iN, iM;
20 
21 /*************************************************************/
22 void iInit()
23 {
24     for (int i = 0; i < iN; i++)
25     {
26         cin >> iNumArr[i];
27     }
28 
29     memset(iSumArr, 0, sizeof(iSumArr));
30 
31     for (int i = 0; i < iN; i++)
32     {
33         for (int j = 0; j < i; j++)
34         {
35             iSumArr[iNumArr[i]+iNumArr[j]]++;
36         }
37     }
38 }
39 
40 void iOutpuResult()
41 {
42     int iCurrent = MAXN - 1;
43 
44     while (iM)
45     {
46         if (iSumArr[iCurrent] > 0)
47         {
48             cout << iCurrent;
49             if (iM != 1)
50             {
51                 cout << " ";
52             }
53             iSumArr[iCurrent]--;
54             iM--;
55         }
56         else
57         {
58             while (iSumArr[iCurrent] == 0)
59             {
60                 iCurrent--;
61             }
62         }
63     }
64     cout << endl;
65 }
66 
67 /*************************************************************/
68 int main()
69 {
70     while (cin >> iN >> iM)
71     {
72         iInit();
73         iOutpuResult();
74     }
75     return 0;
76 }
77 
78 // end
79 // ism
原文地址:https://www.cnblogs.com/ismdeep/p/2603748.html