Snake52996 2017-10-21 01:11:43
点我顶贴
收藏
删除
#include<iostream>
using namespace std;
const int MAX=100010;
long long a[MAX],temp[MAX];
int depart(int begin,int end)
{
int sum=0;
int mid=(begin+end)/2;
if(begin==end) return 0;
else
{
sum+=depart(begin,mid)+depart(mid+1,end);
int i=begin,j=mid+1,p=begin;
while(i<=mid && j<=end)
{
if(a[i]>a[j]) {temp[p++]=a[j++];sum+=mid-i+1;}
else temp[p++]=a[i++];
}
while(j<=end) temp[p++]=a[j++];
while(i<=mid) temp[p++]=a[i++];
for(int i=begin;i<=end;i++) a[i]=temp[i];
return sum;
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
cout<<depart(0,n-1);
}