Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
who can tell me why was it wa so many time?#include<iostream> using namespace std; int sum,a[500017],b[500017]; int me(int low,int mid,int high) { int t=mid,s=low,i,j; //if(mid-low+1>high-mid) // n=mid-low+1; // else n=high-mid; for(i=1;;i++) { if(mid+1>high||low>t) break; if(a[low]<=a[mid+1]) { b[i]=a[low]; low++; } else { sum=sum+(t+1-low); b[i]=a[mid+1]; mid++; } } if(low<=t) for(j=i;;j++) { b[j]=a[low]; low++; if(low>t) break; } else if(mid+1<=high) for(j=i;;j++) { b[j]=a[mid+1]; mid++; if(mid+1>high) break; } for(i=1;i<=j;i++) a[i+s-1]=b[i]; return 0; } int mesort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; mesort(low,mid); mesort(mid+1,high); me(low,mid,high); } return 0; } int main() { int n,i; while(scanf("%d",&n)==1&&n) { sum=0; for(i=1;i<=n;i++) scanf("%d",&a[i]); mesort(1,n); printf("%d\n",sum); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator