http://acm.hdu.edu.cn/showproblem.php?pid=1280
普通方法(625ms)
#include <stdio.h> #include <string.h> #include <stdlib.h> int b[3000*1501]; int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } int main() { int n,m; int a[3001]; while(scanf("%d%d",&n,&m)!=EOF) { int k=0; for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) b[k++]=a[i]+a[j]; } qsort(b,k,sizeof(b[0]),cmp); printf("%d",b[0]); for(int i=1;i<m;i++) { printf(" %d",b[i]); } printf("\n"); } return 0; }