hdu 1280 前m大的数 (计数排序)

hdu1280

题意:给出n(3000)个数,两两求和,输出最大的m(5000)个和。

思路:由于数据有限,想到计数排序。。。以及,m个可能刚好某个数据没有全部输出,要在while里判断一下。。

。。。好好的人。。怎么开始刷水了。。。。。

其实是因为最近在看.suffix array…然后里面涉及到了radix sort..算法课的时候到是写过。。。不过快忘了orz。。所以打算找几道题目。。。? 然而这是计数排序不是基数排序啊摔/!

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月30日 星期六 17时58分04秒
 4File Name :code/hdu/1280.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=3005;
34const int M=1E4+7;
35int n;
36int a[N];
37int cnt[M];
38int m;
39int main()
40{
41	#ifndef  ONLINE_JUDGE 
42	freopen("code/in.txt","r",stdin);
43  #endif
44
45	while (~scanf("%d %d",&n,&m))
46	{
47	    ms(a,0);
48	    ms(cnt,0);
49	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
50	    int mx = 0;
51	    for (  int i = 1 ; i <= n-1 ; i++)
52		for ( int j = i+1 ; j <= n ; j++)
53		    cnt[a[i]+a[j]]++,mx = max(mx,a[i]+a[j]);
54
55//	    cout<<"mx:"<<mx<<endl;
56	    int num = 0 ;
57	    for ( int i = mx ; i >=1&&num<m; i--)
58	    {
59		while (cnt[i]>0)
60		{
61		    num++;
62		    if (num!=m)
63		    printf("%d ",i);
64		    else printf("%d\n",i);
65		    cnt[i]--;
66		    if (num==m) break;
67		}
68	    }
69	 //   puts("");
70	}
71
72  #ifndef ONLINE_JUDGE  
73  fclose(stdin);
74  #endif
75    return 0;
76}