-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountInversions.cpp
More file actions
49 lines (40 loc) · 1.07 KB
/
CountInversions.cpp
File metadata and controls
49 lines (40 loc) · 1.07 KB
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
#include<bits/stdc++.h>
using namespace std;
int merge(vector<int>& a,vector<int>& temp,int left,int mid,int right){
int i,j,k;
int inversions = 0;
i = left;
j = mid;
k = left;
while((i<=mid-1) && j<=right){
if(a[i]<=a[j]) temp[k++] = a[i++];
else{
temp[k++] = a[j++];
inversions+=(mid-i);
}
}
while(i<=mid-1) temp[k++] = a[i++];
while(j<=right) temp[k++] = a[j++];
for(int i=left; i<=right; i++) a[i] = temp[i];
return inversions;
}
int mergeSort(vector<int>& a,vector<int>& temp,int left,int right){
int mid, inversions = 0;
if(right>left){
mid = (left+right)/2;
inversions+=mergeSort(a,temp,left,mid);
inversions+=mergeSort(a,temp,mid+1,right);
inversions+=merge(a,temp,left,mid+1,right);
}
return inversions;
}
int main(){
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
int inversions = 0;
// Using Merge Sort
vector<int> temp = a;
cout << mergeSort(a,temp,0,n-1) << endl;
}