Created
October 16, 2015 10:53
-
-
Save SilverRainZ/5cf2cf647744983e20e9 to your computer and use it in GitHub Desktop.
逆序对
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define N 10005 | |
int count = 0; | |
void Merge(int S[],int T[],int x,int m, int y) | |
{ | |
int i,j,k; | |
for (i = x,j = m + 1,k = x;i <= m && j <= y; k++) | |
{ | |
if (S[i] > S[j]) {T[k] = S[j++];count += m - i + 1;} | |
else T[k] = S[i++]; | |
} | |
while (i <= m) {T[k++] = S[i++];} | |
while (j <= y) T[k++] = S[j++]; | |
} | |
void MergeSort(int S[], int T[],int x, int y) | |
{ | |
int m; | |
int temp[N] = {0}; | |
if (x == y) T[x] = S[y]; | |
else | |
{ | |
m = (x + y)/2; | |
MergeSort(S,temp,x,m); | |
MergeSort(S,temp,m + 1,y); | |
Merge(temp,T,x,m,y); | |
} | |
} | |
int main() | |
{ | |
int L[N]; | |
int n; | |
scanf("%d",&n); | |
for (int i = 1; i <= n; i++) | |
scanf("%d",&L[i]); | |
MergeSort(L,L,1,n); | |
for (int i = 1; i <= n; i++) printf("%d ",L[i]); | |
printf("%d",count); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment