-
Notifications
You must be signed in to change notification settings - Fork 2
/
inversion_count_merge_sort.cpp
112 lines (86 loc) · 2.18 KB
/
inversion_count_merge_sort.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, the size of array. The second line of each test case contains N elements.
Output:
Print the inversion count of array.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 107
1 ≤ C ≤ 1018
Example:
Input:
1
5
2 4 1 3 5
Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
*/
#include<bits/stdc++.h>
using namespace std;
long long invCount;
void merge(vector<long long> &v, long long l, long long mid, long long r){
long long n1=mid-l+1;
long long n2=r-(mid+1)+1;
vector<long long> L(n1);
vector<long long> R(n2);
for(long long i=0;i<n1;i++)
L[i]=v[l+i];
for(long long j=0;j<n2;j++)
R[j]=v[mid+1+j];
long long i=0, j=0, k=l;
while(i<n1 && j<n2){
if(L[i]<=R[j]){
v[k]=L[i];
i++;
} else {
v[k]=R[j];
j++;
invCount+=n1-i;
}
k++;
}
while(i<n1){
v[k]=L[i];
i++;
k++;
}
while(j<n2){
v[k]=R[j];
j++;
k++;
}
}
void mergeSort(vector<long long> &v, long long l, long long r){
if(l<r){
long long mid=l+(r-l)/2;
mergeSort(v, l, mid);
mergeSort(v, mid+1, r);
merge(v, l, mid, r);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
vector<long long> v;
for(long long i=0;i<n;i++){
long long ch;
cin>>ch;
v.push_back(ch);
}
invCount=0;
mergeSort(v, 0, n-1);
cout<<invCount<<endl;
}
return 0;
}