#include <iostream>
using namespace std;
int a[1000002],b[1000002];
long long cnt=0;
void gbpx(int L,int R)
{
if(L>=R) return ;
int mid=(L+R)/2;
gbpx(L,mid);
gbpx(mid+1,R);
int i=L,j=mid+1,k=L;
while(i<=mid&&j<=R)
{
if(a[i]<=a[j]) b[k++]=a[i++]; //b[k]=a[i]; k++; i++;
else
{
b[k++]=a[j++]; //b[k]=a[j]; k++; j++;
cnt+=mid-i+1;
}
}
while(i<=mid) b[k++]=a[i++];
while(j<=R) b[k++]=a[j++];
for(int k=L;k<=R;k++) a[k]=b[k];
}
int main()
{
int n; cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
gbpx(1,n);
cout << cnt << endl;
//for(int i=1;i<=n;i++) cout<< a[i] << " ";
return 0;
}
c++逆序对
最新推荐文章于 2024-05-10 20:12:33 发布