标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14952 Accepted Submission(s): 9140
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y)(x<y?x:y) 4 const int MAXN=5010; 5 int a[MAXN],b[MAXN],anser,c[MAXN]; 6 void mergesort(int l,int r,int mid){ 7 int i=l,j=mid+1,k=l; 8 while(i<=mid&&j<=r){ 9 if(a[i]<=a[j])b[k++]=a[i++]; 10 else{ 11 anser+=j-k; 12 b[k++]=a[j++]; 13 } 14 } 15 while(i<=mid)b[k++]=a[i++]; 16 while(j<=r)b[k++]=a[j++]; 17 for(i=l;i<=r;i++)a[i]=b[i]; 18 } 19 void merge(int l,int r){ 20 if(l<r){ 21 int mid=(l+r)>>1; 22 merge(l,mid); 23 merge(mid+1,r); 24 mergesort(l,r,mid); 25 } 26 } 27 int main(){ 28 int N; 29 while(~scanf("%d",&N)){ 30 for(int i=0;i<N;i++)scanf("%d",a+i),c[i]=a[i]; 31 anser=0; 32 merge(0,N-1); 33 int temp=anser; 34 //printf("%d\n",anser); 35 for(int i=0;i<N;i++){ 36 temp=temp+(N-1-2*c[i]); 37 //printf("%d %d\n",c[i],temp); 38 anser=MIN(anser,temp); 39 } 40 printf("%d\n",anser); 41 } 42 return 0; 43 } 44 //n-a-1-(a) N-2*a+1;
Minimum Inversion Number(归并排序)
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4814442.html