-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInversion count problem.cpp
76 lines (66 loc) · 1.4 KB
/
Inversion count problem.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
// By 'Anki'
// Inversion count problem using merge sort
#include<bits/stdc++.h>
// #include<thread>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout<<#i<<" = "<<i<<", ";
#define wre(i) cout<<#i<<" = "<<i<<endl;
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000000LL
typedef unsigned long long ull;
#define mod 1000000007
#define N 30
#define F first
#define S second
using u64= uint64_t;
ll count_inversion(vector<ll>&v,int l,int mid,int r){
int i=l,j=mid+1;
vector<ll>tmp;
ll count=0LL;
while(i<=mid&&j<=r){
if(v[i]<=v[j])tmp.pb(v[i]),i++;
else tmp.pb(v[j]),j++,count+=mid-i+1;
}
while(i<=mid)tmp.pb(v[i++]);
while(j<=r)tmp.pb(v[j++]);
int k=0;
for(int i=l;i<=r;i++)v[i]=tmp[k++];
return count;
}
ll merge(vector<ll>&v,int l,int r){
ll ans=0LL;
if(l<r){
ll mid=l+(r-l)/2;
ans+= merge(v,l,mid);
ans+= merge(v,mid+1,r);
ans+= count_inversion(v,l,mid,r);
}
return ans;
}
void solve(){
ll n;
cin>>n;
vector<ll>v(n);
for(auto &x:v)cin>>x;
ll ans=merge(v,0,n-1);
cout<<ans<<"\n";
for(auto x:v)cout<<x<<" ";
}
int main()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll t=1LL;
// cin>>t;
while(t--)
{
solve();
}
}