-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_inversion_counter.cpp
54 lines (35 loc) · 1.06 KB
/
array_inversion_counter.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
#include <bits/stdc++.h>
using namespace std;
int merging_inversions(vector<int> &v, int l, int m, int r){
vector<int> left, right;
for(int i = l; i <= m; ++i) left.push_back(v[i]);
for(int i = m+1; i <= r; ++i) right.push_back(v[i]);
int li = 0, ri = 0, ki = l;
int swaps = 0;
while(li < left.size() && ri < right.size()){
if(left[li] <= right[ri]){
v[ki++] = left[li++];
}
else{
v[ki++] = right[ri++];
swaps += (m+1)-(l+li);
}
}
while(li < left.size()) v[ki++] = left[li++];
while(ri < right.size()) v[ki++] = right[ri++];
return swaps;
}
int inversions(vector<int> &v, int l, int r){
if(l >= r) return 0;
int res = 0;
int m = l + (r-l)/2;
res += inversions(v, l, m);
res += inversions(v, m+1, r);
res += merging_inversions(v, l, m, r);
return res;
}
int Solution::countInversions(vector<int> &v) {
int n = v.size();
int ans = inversions(v, 0, n-1);
return ans;
}