-
Notifications
You must be signed in to change notification settings - Fork 4
/
180.cpp
47 lines (47 loc) · 970 Bytes
/
180.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
/*
*NAME:Inversions
*LANG:C++
*SOURCE:sgu180
*METHOD:归并排序求逆序数
*Times:2,ans没开long long 多交了一次。。
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn = 66666,INF = 0x7FFFFFFF;
int a[maxn];
long long ans,n;
void Merge(int p,int q,int r){
int n1 = q-p+1,n2 = r-q;
int L[maxn],R[maxn];
for (int i=1;i<=n1;++i) L[i] = a[p+i-1];L[n1+1]=INF;
for (int i=1;i<=n2;++i) R[i] = a[q+i];R[n2+1]=INF;
int i =1,j=1;
for (int k=p;k<=r;++k){
if (L[i]<=R[j]){
a[k] = L[i];
i++;
}
else{
a[k] = R[j];
j++;ans+=(n1-i+1);
}
}
}
void MergeSort(int p,int r){
if (p<r){int q = (p+r)/2;
MergeSort(p,q);
MergeSort(q+1,r);
Merge(p,q,r);
}
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
cin >> n;
for (int i=1;i<=n;++i)
cin >> a[i];
MergeSort(1,n);
cout << ans << endl;
}