-
Notifications
You must be signed in to change notification settings - Fork 0
/
invcnt.cpp
88 lines (84 loc) · 2.21 KB
/
invcnt.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<cstdio>
int findPos(int *array, int lIndex, int rIndex, int val) {
if(lIndex > rIndex) {
return lIndex;
}
if(lIndex == rIndex) {
if(val >= array[lIndex]) {
return lIndex + 1;
} else {
return lIndex;
}
}
int mid = (lIndex + rIndex)/2;
if(val >= array[mid]) {
return findPos(array, mid + 1, rIndex, val);
} else {
return findPos(array, lIndex, mid - 1, val);
}
}
long long computeInversionsSorted(int *array, int lIndex, int mid, int rIndex) {
long long inv = 0;
for(int i=mid + 1;i<=rIndex;++i) {
int pos = findPos(array, lIndex, mid, array[i]);
inv += mid - pos + 1;
}
return inv;
}
void mergeArrays(int* array, int lIndex, int mid, int rIndex) {
int p = lIndex;
int q = mid + 1;
int *mergeArray = new int[rIndex - lIndex + 1];
int index = 0;
while(p<=mid && q <= rIndex) {
if(array[p] < array[q]) {
mergeArray[index] = array[p];
p++;
} else {
mergeArray[index] = array[q];
q++;
}
index++;
}
while(p<=mid) {
mergeArray[index] = array[p];
p++;
index++;
}
while(q <= rIndex) {
mergeArray[index] = array[q];
q++;
index++;
}
for(int i=0;i<(rIndex - lIndex + 1);++i) {
array[lIndex + i] = mergeArray[i];
}
delete [] mergeArray;
}
long long countInversions(int *array, int lIndex, int rIndex) {
if(rIndex == lIndex) {
return 0;
}
int mid = (lIndex + rIndex)/2;
long long inv = countInversions(array, lIndex, mid);
inv += countInversions(array, mid + 1, rIndex);
// compute the inversions for the two halves
long long val = computeInversionsSorted(array, lIndex, mid, rIndex);
mergeArrays(array, lIndex, mid, rIndex);
return val + inv;
}
int main() {
int test;
scanf("%d", &test);
while(test--) {
int n;
scanf("%d", &n);
int *array = new int[n];
for(int i=0;i<n;++i) {
scanf("%d", array + i);
}
long long inversions = countInversions(array, 0, n -1);
printf("%lld\n", inversions);
delete [] array;
}
}