逆序对个数
#include <iostream>
using namespace std;
const int N = 1e5 + 11;
int a[N],b[N];
void merge_sort(int a[],int l,int r){
if(l >= r) return;
int mid = (l+r) >> 1;
merge_sort(a,l,mid);
merge_sort(a,mid+1,r);
int k = 0;
int i = l;
int j = mid+1;
while(i <= mid && j <= r)
if(a[i] <= a[j]) b[k++] = a[i++];
else b[k++] = a[j++];
while(i <= mid) b[k++] = a[i++];
while(j <= r) b[k++] = a[j++];
for(int i = l,k = 0; i <= r; i++,k++) a[i] = b[k];
}
int main(){
int n; cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
merge_sort(a,0,n-1);
for(int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
逆序对个数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/48275884f9858eba35b61375bae39561.png)