用归并排序的思路分析这道题
接下来上代码
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int N=10e6+10;
ll count=0;
ll a[N],tmp[N];
void merge(int l,int r)
{
if(l>=r)return;
int mid=l+r>>1;
merge(l,mid),merge(mid+1,r);
int i=l,j=mid+1,k=0;
while(i<=mid&&j<=r)
if(a[i]<=a[j])tmp[k++]=a[i++];
else
{
count+=mid-i+1;
tmp[k++]=a[j++];
}
while(i<=mid)tmp[k++]=a[i++];
while(j<=r)tmp[k++]=a[j++];
for(int i=l,j=0;i<=r;i++,j++)
a[i]=tmp[j];
}
int main()
{
int n=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
merge(0,n-1);
cout<<count;
}