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

hdu1280

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

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

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

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

/* ***********************************************
Author :111qqz
Created Time :2016年07月30日 星期六 17时58分04秒
File Name :code/hdu/1280.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=3005;
 7const int M=1E4+7;
 8int n;
 9int a[N];
10int cnt[M];
11int m;
12int main()
13{
14	#ifndef  ONLINE_JUDGE 
15	freopen("code/in.txt","r",stdin);
16  #endif
1	while (~scanf("%d %d",&n,&m))
2	{
3	    ms(a,0);
4	    ms(cnt,0);
5	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
6	    int mx = 0;
7	    for (  int i = 1 ; i <= n-1 ; i++)
8		for ( int j = i+1 ; j <= n ; j++)
9		    cnt[a[i]+a[j]]++,mx = max(mx,a[i]+a[j]);
 1//	    cout<<"mx:"<<mx<<endl;
 2	    int num = 0 ;
 3	    for ( int i = mx ; i >=1&&num<m; i--)
 4	    {
 5		while (cnt[i]>0)
 6		{
 7		    num++;
 8		    if (num!=m)
 9		    printf("%d ",i);
10		    else printf("%d\n",i);
11		    cnt[i]--;
12		    if (num==m) break;
13		}
14	    }
15	 //   puts("");
16	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}