http://acm.hdu.edu.cn/showproblem.php?pid=1280
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6323 Accepted Submission(s): 2235
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
第一行两个数N和M,
第二行N个数,表示该序列。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; int a[3010],b[10010],res[3010]; int main(){ //freopen("input.txt","r",stdin); int n,m; while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++) scanf("%d",&a[i]); memset(b,0,sizeof(b)); for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) b[a[i]+a[j]]++; int cnt=0; for(int i=10000;i>=0;i--){ while(b[i]!=0){ res[cnt++]=i; b[i]--; if(cnt==m) break; } if(cnt==m) break; } for(int i=0;i<m-1;i++) printf("%d ",res[i]); printf("%d\n",res[m-1]); } return 0; }