NC15163逆序数
作者:互联网
这其实就是一个归并排序
#include<iostream>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int a[100009], b[100009];
long long ans = 0;
void Merge_Sort(int l, int mid, int r) {
int p1 = l, p2 = mid + 1;
for (int i = l; i <= r;i++) {
if ((p1 <= mid) && ((p2 > r) || (a[p1] <= a[p2]))) {
b[i] = a[p1];
p1++;
}
else {
b[i] = a[p2];
p2++;
ans += mid - p1 + 1;
}
}
for (int i = l;i <= r;i++) {
a[i] = b[i];
}
}
void dichotomy(int l, int r) {
int mid = (l + r) >> 1;
if (l < r) {
dichotomy(l, mid);
dichotomy(mid + 1, r);
}
Merge_Sort(l, mid, r);
}
int main() {
IOS;
int n;
cin >> n;
for (int i = 0;i < n;i++) {
cin >> a[i];
}
dichotomy(0, n - 1);
cout << ans << endl;
return 0;
}
标签:p2,p1,NC15163,dichotomy,int,mid,++,序数 来源: https://blog.csdn.net/sunhongxuan666/article/details/113827934