LumosElara @ 2024-05-06 15:47:42
#include <iostream>
#include <vector>
using namespace std;
void Merge(vector<int> &r, int s, int m, int t, int &sum) {
vector<int> r1(t - s + 1);
int i = s, j = m + 1, k = 0;
while (i <= m && j <= t) {
if (r[i] <= r[j])
r1[k++] = r[i++];
else {
r1[k++] = r[j++];
sum += (m - i + 1);
}
}
while (i <= m)
r1[k++] = r[i++];
while (j <= t)
r1[k++] = r[j++];
for (int j = 0, i = s; j < k; i++, j++) {
r[i] = r1[j];
}
}
void MergeSort(vector<int> &r, int s, int t, int &sum) {
if (s >= t) return;
int m = (s + t) / 2;
MergeSort(r, s, m, sum);
MergeSort(r, m + 1, t, sum);
Merge(r, s, m, t, sum);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
MergeSort(a, 0, n - 1, sum);
cout << sum<<endl;
return 0;
}
by LumosElara @ 2024-05-06 15:53:17
通过了谢谢,ans应该是long long
by ___Furina___ @ 2024-05-06 15:53:50
@LLL_flying 开
by LumosElara @ 2024-05-08 14:32:50
@OIer_qyzy 好的非常感谢