归并排序逆序对
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int a[100],t[100];
inline void merge_sort(int l,int r)
{
if(l==r) return ;
int mid = (l+r)>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int p1=l,p2=mid+1;
for(int i=l;i<=r;i++)
{
if(p1<=mid && p2<=r)
{
if(a[p1]<=a[p2]) t[i] = a[p1++];
else
{
t[i] = a[p2++];
ans += mid-p1+1;
}
}
else
{
if(p1<=mid) t[i] = a[p1++];
else t[i] = a[p2++];
}
}
for(int i=l;i<=r;i++) a[i] = t[i];
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
merge_sort(1,n);
cout<<ans<<endl;
for(int i=1;i<=n;i++) cout<<a[i]<<' ';
return 0;
}