forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_inversions.cpp
62 lines (53 loc) · 1.78 KB
/
count_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
57
58
59
60
61
62
#include <iostream>
#include <vector>
//The goal in this problem is to count the number of inversions of a given sequence.
using std::vector;
// Merge two subarrays L and M into arr
int merge_sort(vector<int> &arr, vector<int> &temp, size_t left, size_t mid, size_t right)
{
int i, j, k;
int inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
long long get_number_of_inversions(vector<int> &arr, vector<int> &b, size_t left, size_t right) {
long long number_of_inversions = 0;
if (right <= left + 1) return number_of_inversions;
size_t ave = left + (right - left) / 2;
number_of_inversions += get_number_of_inversions(arr, b, left, ave);
number_of_inversions += get_number_of_inversions(arr, b, ave, right);
number_of_inversions += merge_sort(arr, b, left, ave, right);
return number_of_inversions;
}
int main() {
int n;
std::cin >> n;
vector<int> a(n);
for (size_t i = 0; i < a.size(); i++) {
std::cin >> a[i];
}
vector<int> b(a.size());
std::cout << get_number_of_inversions(a, b, 0, a.size()) << '\n';
}