-
Notifications
You must be signed in to change notification settings - Fork 1
/
total_inversions.cpp
56 lines (54 loc) · 1.21 KB
/
total_inversions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
long long int merge(long long arr[],long long l,long long mid,long long r){
long long int inv=0;
long long tarr[r-l+1];
long long tl=0,tmid=mid-l,tr=r-l;
for(long long i=0;i<r-l+1;i++){
tarr[i]=arr[i+l];
}
for(long long i=0;i<r-l+1;i++){
if(tl>mid-l){
arr[i+l]=tarr[tmid+1];
tmid++;
continue;
}
if(tmid+1>r-l){
arr[i+l]=tarr[tl];
tl++;
continue;
}
if(tarr[tl]>tarr[tmid+1]){
inv+=mid-l-tl+1;
arr[i+l]=tarr[tmid+1];
tmid++;
}
else{
arr[i+l]=tarr[tl];
tl++;
}
}
return inv;
}
long long int mergesort(long long arr[],long long l,long long r){
long long int a=0;
if(l>=r) return 0;
long long mid=(l+r)/2;
a+=mergesort(arr,l,mid);
a+=mergesort(arr,mid+1,r);
a+=merge(arr,l,mid,r);
return a;
}
int main(){
long long n;
cin>>n;
long long int arr[n];
for(long long i=0;i<n;i++){
cin>>arr[i];
}
cout<<mergesort(arr,0,n-1)<<endl;
for(long long i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}