shǎ崽 OrOrOrOrz
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4519 Accepted Submission(s): 2161
Problem Description
Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing problems overnight. So many girls want to meet and Orz him. But Orz him is not that easy.You must solve this problem first.
The problem is :
Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen .
For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
The problem is :
Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen .
For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
Input
There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.
Output
Output a sequence of distinct integers described above.
Sample Input
5 1 2 3 4 5
Sample Output
5 1 4 2 3// 归并排序 加了优化的 78ms#include<stdio.h> #include<math.h> int a[10001],x1[10001],x2[10001]; void insort(int a[],int L,int R) { int i,j,temp; for(i=L+1;i<=R;i++) { if(a[i]<a[i-1]) { temp=a[i]; j=i-1; do { a[j+1]=a[j]; j--; }while(temp<a[j]); } a[j+1]=temp; } } void merge(int a[],int L,int M,int R) { int i,j,k,len1,len2; len1=M-L+1; len2=R-M; for(i=0;i<len1;i++) x1[i]=a[L+i]; for(i=0;i<len2;i++) x2[i]=a[M+1+i]; for(i=0,j=0,k=L;i<len1&&j<len2;) if(x1[i]<x2[j]) a[k++]=x1[i++]; else a[k++]=x2[j++]; while(i<len1) a[k++]=x1[i++]; while(j<len2) a[k++]=x2[j++]; } void divide(int a[],int L,int R) { if(L<R) { int len=R-L+1;//当前的序列长度 int k=log(len);//k是使得 //插入排序比归并排序快的 //最大列表长度。 int M=L+((R-L)>>1); //求中值, //这样算避免中间数值越界 if((R-L)>=(k*2)) //判断用归并 //还是直接插入排序 { divide(a,L,M); divide(a,M+1,R); } else { insort(a,L,M); insort(a,M+1,R); } merge(a,L,M,R); } } int main() { int n,i,j; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",a+i); divide(a,0,n-1); //调用归并函数 //输出题目要求的结果 for(i=0,j=n-1;i<n/2;i++,j--) { if(i!=0) printf(" "); printf("%d %d",a[j],a[i]); } if(n&1) printf(" %d",a[n/2]); printf("\n"); } return 0; }