-
Notifications
You must be signed in to change notification settings - Fork 17
/
counting_inversion.cpp
95 lines (80 loc) · 2 KB
/
counting_inversion.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
#include<iostream>
#include<vector>
#include<algorithm>
#include<bitset>
#include<map>
#include<set>
#include<queue>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
#define ll long long int
#define it(a) ::iterator a
#define sd(a) double a;scanf("%lf",&a);
#define slld(a) ll a;scanf("%lld",&a)
#define plld(a) printf("%lld",a)
#define INF LLONG_MAX
#define all(a) a.begin(), a.end()
#define notIn(M,a) M.find(a) == M.end()
#define MOD 1000000007
#define powOf2(a) !(a&a-1)
#define tc(a) ll a; for(scanf("%lld",&a);a--;)
#define repd(i,a,n) for(ll i = n-1;i>=a;i--)
#define rep(i,a,n) for(ll i = a;i<n;i++)
#define toLower(s, len) rep(i, 0, len) s[i] = (s[i]>='A' && s[i]<='Z' ? s[i]-'A'+'a' : s[i]);
#define toUpper(s, len) rep(i, 0, len) s[i] = (s[i]>='a' && s[i]<='z' ? s[i]-'a'+'A' : s[i]);
#define set(n,x) x |= (1 << n)
#define unset(n,x) x &= ~(1<<n)
#define check(n,x) (x & 1 << n) != 0
#define ppi pair<ll, pair<ll, ll> >
ll inv = 0;
vector<ll> B;
int merge(ll start, ll mid, ll end, vector<ll> &V){
//cout<<"start : "<<start<<" mid : "<<mid<<" end: "<<end<<endl;
for(ll i = start;i<=end;i++){
B[i] = V[i];
}
ll i = start,j = mid+1, k = start;
while(k <= end){
//cout<<" i : "<<i<<" j : "<<j<<" k : "<<k<<endl;
if(i > mid) V[k++] = B[j++];
else if(j > end) V[k++] = B[i++];
else if(B[i] > B[j]){
V[k++] = B[j++];
inv += ((mid-i+1));
}
else V[k++] = B[i++];
}
return 0;
}
int mergesort(ll start, ll end, vector<ll> &V){
if(start == end) return 0;
//cout<<"start :"<<start<<" end : "<<end<<endl;
ll mid = (start+end)/2;
mergesort(start, mid, V);
mergesort(mid+1, end, V);
merge(start, mid, end, V);
return 0;
}
int solve(){
slld(n);
vector<ll> V;
rep(i, 0, n){
slld(temp);
V.push_back(temp);
}
inv = 0;
rep(i, 0, n+1) B.push_back(0);
mergesort(0, n-1, V);
//for(ll i = 0;i<n;i++) cout<<V[i]<<" ";
//cout<<endl;
printf("%lld\n", inv);
return 0;
}
int main(){
solve();
return 0;
}