Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Let and define . Then the value of an inversion is just . That already smells much cleaner than raw interval sums.
Donβt think about the permutation values directly. Think about the order of positions by decreasing value. If position gets a larger number than position , then in that order, comes before .
Now isolate just two positions and . Compare the total beauty if comes before versus if comes before . Surprise: the difference depends only on and , not on anything else.
Work out that difference carefully. If the order is before , and you swap them, the beauty changes by . So if , having before is strictly worse. Thatβs your exchange argument screaming at you.
So in an optimal order, positions must be sorted by nondecreasing . After sorting indices by this key, assign values in that order. Ties are free: any order among equal prefix sums works.
This is one of those problems that looks like nasty inversion-interval soup, but the whole thing collapses after one good re-encoding.
Let
So , , , and so on.
Then for any inversion with , its value is
So the beauty is
Thatβs already much less cursed.
Instead of constructing the values of the permutation directly, look at the positions in order of decreasing permutation value.
Let
be the positions such that
This order completely determines the permutation: the first position gets value , the second gets , etc.
So now the problem is:
Take any two positions and .
What happens if is before in versus before ?
Thus
Now the relevant pair is .
So again,
Same formula. Very nice. Very suspiciously nice.
Suppose in the order two adjacent positions are .
If we swap them, all other pairs stay in the same relative order. So the only change in beauty comes from this pair.
If the current order is before , then swapping gives change
So:
That means the optimal order must have
Boom. The whole problem is just sorting positions by .
If some are equal, any relative order among them is optimal, because swapping equal keys changes nothing.
The exchange argument is enough:
Thatβs it. No DP, no segment tree circus, no black magic. Just prefix sums and sorting.
For each test case:
Total complexity over all test cases is
which is easily fine for .
Memory usage is .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
vector<pair<ll, int>> v;
v.reserve(n);
ll pref = 0;
for (int i = 1; i <= n; ++i) {
v.push_back({pref, i}); // r_i = sum of a_1..a_{i-1}
pref += a[i];
}
sort(v.begin(), v.end());
vector<int> p(n + 1);
for (int i = 0; i < n; ++i) {
p[v[i].second] = n - i;
}
for (int i = 1; i <= n; ++i) {
if (i > 1) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
vector<pair<ll, int>> v;
v.reserve(n);
ll pref = 0;
for (int i = 1; i <= n; ++i) {
v.push_back({pref, i}); // r_i = sum of a_1..a_{i-1}
pref += a[i];
}
sort(v.begin(), v.end());
vector<int> p(n + 1);
for (int i = 0; i < n; ++i) {
p[v[i].second] = n - i;
}
for (int i = 1; i <= n; ++i) {
if (i > 1) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}