AcWing 5909. 车厢重组
原题链接
简单
作者:
D.Russell
,
2024-11-23 20:38:55
,
所有人可见
,
阅读 13
直接算逆序对即可 改都不用改
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
const int N = 1e6;
int q[N], tmp[N];
int n;
LL m_sort(int q[], int l, int r) {
if (l >= r) return 0;
int mid = l + r >> 1;
LL res = m_sort(q, l, mid) + m_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r) {
if (q[i] <= q[j]) tmp[k ++] = q[i ++];
else {
tmp[k ++] = q[j ++];
res += mid - i + 1;
}
}
while (i <= mid) tmp[k ++] = q[i ++];
while (j <= r) tmp[k ++] = q[j ++];
for (i = l, j = 0; i <= r; i ++, j ++) q[i] = tmp[j];
return res;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++) cin >> q[i];
cout << m_sort(q, 0, n - 1);
return 0;
}