-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge sort and inversion counting
70 lines (64 loc) · 1.12 KB
/
Merge sort and inversion counting
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
#include <bits/stdc++.h>
using namespace std;
vector<int>v;
int n;
long long int count_inversion(int beg,int en)
{
if(beg>=en)
return 0;
int md;
md=(beg+en)/2;
long long int inversion_left=count_inversion(beg,md);
long long int inversion_right=count_inversion(md+1,en);
vector<int>tmp(en-beg+1);
long long int inversion_merge=0;
int i=beg,j=md+1,k=0;
while(i<=md&&j<=en)
{
if(v[i]<=v[j])
{
tmp[k]=v[i];
i++;
}
else
{
inversion_merge=inversion_merge+(md-i+1);
tmp[k]=v[j];
j++;
}
k++;
}
while(i<=md)
{
tmp[k]=v[i];
i++;
k++;
}
while(j<=en)
{
tmp[k]=v[j];
j++;
k++;
}
i=0;
while(beg<=en)
{
v[beg]=tmp[i];
i++;
beg++;
}
tmp.clear();
return inversion_left+inversion_right+inversion_merge;
}
int main()
{
int i,j;
cin>>n;
for(i=0;i<n;i++)
{
cin>>j;
v.push_back(j);
}
cout<<count_inversion(0,n-1)<<endl;
return 0;
}