-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sort.cpp
More file actions
52 lines (47 loc) · 1.35 KB
/
merge-sort.cpp
File metadata and controls
52 lines (47 loc) · 1.35 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
49
50
51
52
//
// Merge Sort: Counting Inversions
// https://www.hackerrank.com/challenges/ctci-merge-sort
//
// For each of the datasets, print the number of inversions that must be swapped to sort the dataset.
// Only adjacent elements can be swapped.
//
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct MergeCount {
long long swaps = 0;
template<class It>
void sort(It begin, It end) {
int n = end - begin;
if (n < 2) return;
It m = begin + n / 2;
sort(begin, m);
sort(m, end);
merge(begin, m, end);
}
template<class It>
void merge(It begin, It m, It end) {
vector<int> tmp;
tmp.reserve(end - begin);
for (It l = begin, r = m; l < m || r < end;) {
while (l < m && (r == end || *l <= *r)) tmp.push_back(*l++);
while (r < end && (l == m || *r >= *l)) tmp.push_back(*r++);
while (r < end && (l == m || *r < *l)) tmp.push_back(*r++), swaps += m - l;
}
copy(tmp.begin(), tmp.end(), begin);
}
};
int main() {
int t; cin >> t;
for (int i = 0; i < t; i++) {
int n; cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
MergeCount m;
m.sort(arr.begin(), arr.end());
cout << m.swaps << endl;
}
return 0;
}