标签:
1 #include <stdio.h> 2 #define MAX 1000000 3 4 int arr[MAX+5],ass[MAX+5]; 5 __int64 cnt; 6 7 void MergeSort(int low,int high){ 8 int mid,p,q,i; 9 if(high-low==1) 10 return; 11 mid=low+((high-low)>>1); 12 MergeSort(low,mid); 13 MergeSort(mid,high); 14 p=low,q=mid,i=low; 15 while(p<mid||q<high){ 16 if(q>=high||(p<mid&&arr[p]<=arr[q])) 17 ass[i++]=arr[p++]; 18 else{ 19 ass[i++]=arr[q++]; 20 cnt+=mid-p; 21 } 22 } 23 for(i=low;i<high;i++) 24 arr[i]=ass[i]; 25 } 26 27 void scani(int &num){ 28 char ch; 29 int flag; 30 while(ch=getchar(),(ch>‘9‘||ch<‘0‘)&&(ch!=‘-‘)); 31 if(ch==‘-‘) 32 flag=-1,num=0; 33 else 34 flag=1,num=ch-‘0‘; 35 while(ch=getchar(),ch<=‘9‘&&ch>=‘0‘) 36 num=num*10+ch-‘0‘; 37 num*=flag; 38 } 39 40 int main(){ 41 int n,i; 42 while(~scanf("%d",&n)){ 43 for(i=1;i<=n;i++) 44 scani(arr[i]); 45 cnt=0; 46 MergeSort(1,n+1); 47 printf("%I64d\n",cnt); 48 } 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/to-creat/p/4843294.html