#include <bits/stdc++.h>
using namespace std;
int n;
long long num;
int pp[100010], tmp[100010];
void Msort(int l, int r);
void Merge( int l, int e,int m, int r);
int main()
{
num = 0;
cin >> n;
for(int i = 0; i < n; i++)
{
scanf("%d", &pp[i]);
}
Msort(0, n-1);
printf("%lld\n",num);
return 0;
}
void Msort(int l, int r)
{
if(l < r)
{
int mid = (l+r)/2;
Msort(l, mid);
Msort(mid+1, r);
Merge(l, mid, mid+1, r);
}
}
void Merge(int l, int e, int m, int r)
{
int s1 = l, s2 = m;
int k = 0;
while(s1 <= e && s2 <= r)
{
if(pp[s1] <= pp[s2])
{
tmp[k++] = pp[s1++];
}
else
{
tmp[k++] = pp[s2++];
num += e-s1+1;
}
}
while(s1 <= e)
{
tmp[k++] = pp[s1++];
}
while(s2 <= r)
{
tmp[k++] = pp[s2++];
}
/*for(int i = 0; i < r-l+1; i++,r--)
{
pp[r] = tmp[r];
}*/
for(int i=l;i<=r;i++)
{
pp[i]=tmp[i-l];
}
}
SDUT 3402 数据结构实验之排序五:归并求逆序数
最新推荐文章于 2019-07-24 08:45:28 发布