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.
For each pair , write out the four possible choices: stay/stay, stay/mirror, mirror/stay, mirror/mirror. The comparison becomes much simpler than it looks.
If the left element is mirrored and the right element stays, that pair is always an inversion. If the left stays and the right is mirrored, that pair is never an inversion.
For same-choice pairs: stay/stay acts like the original permutation, while mirror/mirror reverses value order because iff .
Now focus on the smaller value in a pair. If the larger value is to its left, choosing the smaller value as stay creates the inversion. If the larger value is to its right, choosing the smaller value as mirror creates the inversion.
So each position can be decided independently: for value , staying costs the number of larger values to its left, mirroring costs the number of larger values to its right. Add over all positions.
Let an index be stay if we choose , and mirror if we choose .
For a pair , set and . There are only four cases:
| choice at | choice at | inversion? | |---|---|---| | stay | stay | yes iff | | stay | mirror | never | | mirror | stay | always | | mirror | mirror | yes iff |
The mixed cases are the important part: every mirrored value is at least as large as every stayed value, and since is a permutation, the only equality edge case does not create trouble. So mirror/stay always loses, stay/mirror always behaves.
Now deconstruct the pair by asking: where is the smaller value?
The smaller value is at index , on the right.
The pair contributes:
The choice of index does not matter for this pair. Weird-looking, but true from the table.
So if a value has a larger value to its left, choosing stay pays for that larger value.
The smaller value is at index , on the left.
The pair contributes:
Again, the larger value's choice does not matter.
So if a value has a larger value to its right, choosing mirror pays for that larger value.
For each position , define:
If we choose stay at , we pay .
If we choose mirror at , we pay .
And here is the nice part: these costs are independent for every index. No DP, no graph cut, no cursed bitmask nonsense. Each inversion pair charges exactly one endpoint: the endpoint with the smaller permutation value.
Therefore the minimum possible number of inversions is
Since , an implementation is completely fine: for each , scan left and right and count larger values.
Per test case:
#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<int> p(n);
for (int &x : p) cin >> x;
ll ans = 0;
for (int i = 0; i < n; i++) {
int largerLeft = 0, largerRight = 0;
for (int j = 0; j < i; j++) {
if (p[j] > p[i]) largerLeft++;
}
for (int j = i + 1; j < n; j++) {
if (p[j] > p[i]) largerRight++;
}
ans += min(largerLeft, largerRight);
}
cout << ans << '\n';
}
}#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<int> p(n);
for (int &x : p) cin >> x;
ll ans = 0;
for (int i = 0; i < n; i++) {
int largerLeft = 0, largerRight = 0;
for (int j = 0; j < i; j++) {
if (p[j] > p[i]) largerLeft++;
}
for (int j = i + 1; j < n; j++) {
if (p[j] > p[i]) largerRight++;
}
ans += min(largerLeft, largerRight);
}
cout << ans << '\n';
}
}