神秘族首领 @ 2023-07-26 10:38:13
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[500010];
ll n;
int ans = 0;
void merge(ll left, ll right) {
ll mid = (left + right) / 2;
ll l[n + 2];
ll r[n + 2];
for (ll i = left; i <= mid; i++) {
l[i] = a[i];
}
for (ll i = mid + 1; i <= right; i++) {
r[i] = a[i];
}
l[mid + 1] = 99999999;
r[right + 1] = 99999999;
ll templ = left;
ll tempr = mid + 1;
for (ll i = left; i <= right; i++) {
if (l[templ] <= r[tempr]) {
a[i] = l[templ];
//ans += templ - left + tempr - right;
templ++;
} else {
a[i] = r[tempr];
//ans++;
ans += mid - templ + 1;
tempr++;
}
}
}
void mergesort(ll left, ll right) {
ll mid = (left + right) / 2;
if (left < right) {
mergesort(left, mid);
mergesort(mid + 1, right);
merge(left, right);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
mergesort(1, n);
// for (int i = 1; i <= n; i++)
// cout << a[i] << " ";
//cout << endl;
cout << ans ;
return 0;
}
by 神秘族首领 @ 2023-07-26 11:23:55
没事了,测试数据应该都是至少1000个数以上,并且每个数很大很大
by 神秘族首领 @ 2023-07-26 11:24:16
l[mid + 1] = 99999999; r[right + 1] = 99999999; 这两行开的太小了
by 神秘族首领 @ 2023-07-26 11:24:47
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[500010];
ll n;
ll ans = 0;
void merge(ll left, ll right) {
ll mid = (left + right) / 2;
ll l[n + 2];
ll r[n + 2];
for (ll i = left; i <= mid; i++) {
l[i] = a[i];
}
for (ll i = mid + 1; i <= right; i++) {
r[i] = a[i];
}
l[mid + 1] = 1e15;
r[right + 1] = 1e15;
ll templ = left;
ll tempr = mid + 1;
for (ll i = left; i <= right; i++) {
if (l[templ] <= r[tempr]) {
a[i] = l[templ];
//ans += templ - left + tempr - right;
templ++;
} else {
a[i] = r[tempr];
//ans++;
ans += mid - templ + 1;
tempr++;
}
}
}
void mergesort(ll left, ll right) {
ll mid = (left + right) / 2;
if (left < right) {
mergesort(left, mid);
mergesort(mid + 1, right);
merge(left, right);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
mergesort(1, n);
// for (int i = 1; i <= n; i++)
// cout << a[i] << " ";
//cout << endl;
cout << ans ;
return 0;
}
这样就能过了