標籤:style blog http color os art
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;}View Code
雜湊(15ms)
#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){ int n,m; int a[3001],b[10001]; while(scanf("%d%d",&n,&m)!=EOF) { memset(b,0,sizeof(b)); 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[a[i]+a[j]]++; } int flag=0; int l=0; for(int i=10000;i>=0;i--) { while(b[i]) { if(flag==0) { printf("%d",i); flag=1; } else printf(" %d",i); b[i]--; l++; if(l==m) break; } if(l==m) break; } printf("\n"); } return 0;}
http://acm.hdu.edu.cn/showproblem.php?pid=1425
#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 500000int b[1000001];int main(){ int n,m,t; while(scanf("%d%d",&n,&m)!=EOF) { memset(b,0,sizeof(b)); for(int i=0;i<n;i++) { scanf("%d",&t); if(t<0) b[t+N]++; else b[t+N]++; } int flag=0; int l=0; for(int i=1000000;i>=0;i--) { while(b[i]) { if(flag==0) { printf("%d",i-N); flag=1; } else printf(" %d",i-N); b[i]--; l++; if(l==m) break; } if(l==m) break; } printf("\n"); } return 0;}View Code