标签:des style blog color strong width
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 118 | Accepted: 28 |
Description
对于一个包含N个非负整数的数组A[1..n],如果有i < j,且A[ i ]>A[ j ],则称(A[ i] ,A[ j] )为数组A中的一个逆序对。
Input
Output
Sample Input
Sample Output
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 6 7 int a[1000005]; 8 int b[1000005]; 9 __int64 ans; 10 11 void bing(int left,int mid,int right) 12 { 13 int i, j, k; 14 for(i=left;i<=right;i++) 15 b[i]=a[i]; 16 i=left;j=mid+1;k=left; 17 while(i<=mid&&j<=right) 18 { 19 if(b[i]<=b[j]) a[k++]=b[i++]; 20 else{ 21 a[k++]=b[j++]; 22 ans+=mid-i+1; 23 } 24 } 25 while(i<=mid) a[k++]=b[i++]; 26 while(j<=right) a[k++]=b[j++]; 27 } 28 29 void msort(int left,int right) 30 { 31 if(left<right) 32 { 33 34 int mid=(left+right)/2; 35 msort(left,mid); 36 msort(mid+1,right); 37 bing(left,mid,right); 38 } 39 } 40 main() 41 { 42 int n, i; 43 scanf("%d",&n); 44 for(i=1;i<=n;i++) scanf("%d",&a[i]); 45 ans=0; 46 msort(1,n); 47 printf("%I64d\n",ans); 48 }
ZJNU 1247 归并排序求逆序对,布布扣,bubuko.com
标签:des style blog color strong width
原文地址:http://www.cnblogs.com/qq1012662902/p/3834547.html