题意:给出n(3000)个数,两两求和,输出最大的m(5000)个和。
思路:由于数据有限,想到计数排序。。。以及,m个可能刚好某个数据没有全部输出,要在while里判断一下。。
。。。好好的人。。怎么开始刷水了。。。。。
其实是因为最近在看.suffix array…然后里面涉及到了radix sort..算法课的时候到是写过。。。不过快忘了orz。。所以打算找几道题目。。。? 然而这是计数排序不是基数排序啊摔/!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
/* *********************************************** Author :111qqz Created Time :2016年07月30日 星期六 17时58分04秒 File Name :code/hdu/1280.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=3005; const int M=1E4+7; int n; int a[N]; int cnt[M]; int m; int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif while (~scanf("%d %d",&n,&m)) { ms(a,0); ms(cnt,0); for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]); int mx = 0; for ( int i = 1 ; i <= n-1 ; i++) for ( int j = i+1 ; j <= n ; j++) cnt[a[i]+a[j]]++,mx = max(mx,a[i]+a[j]); // cout<<"mx:"<<mx<<endl; int num = 0 ; for ( int i = mx ; i >=1&&num<m; i--) { while (cnt[i]>0) { num++; if (num!=m) printf("%d ",i); else printf("%d\n",i); cnt[i]--; if (num==m) break; } } // puts(""); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!