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.
Ignore the swap for a second. Is there any index that is ugly in every permutation of length ?
For , the prefix contains the whole permutation, so its maximum is always . That gives a hard lower bound of ugly index.
Now try to make every index not ugly. Since ugly means , what prefix maximum would instantly prevent that?
If , then every prefix has maximum . For all , , so none of those indices are ugly.
So the whole construction is just: move the value to position using one swap. If it is already there, do nothing.
The key is that the condition compares an index to a prefix maximum value:
An index is ugly iff
This is not asking whether . That false assumption is the tiny trap here. Do not overcook this into checking all swaps; that is a very Codeforces way to waste five minutes and learn nothing.
First, there is an unavoidable lower bound. At index , the prefix is the entire permutation, so
Therefore index is always ugly. No permutation can have fewer than ugly index.
Now we show this lower bound is achievable. Put the largest value at the first position. Then for every prefix, the maximum is already :
For every , we have , so those indices are not ugly. For , we get , so exactly one index is ugly.
Because is a permutation, the value appears exactly once. If it is at position , swap and . This uses one operation if , and zero operations if . Both are allowed because the statement says at most once.
So the answer is simply the original permutation after moving to the front.
Proof of optimality:
Edge cases:
Complexity is per test case to read the array and find , with extra space besides the array.
Research checked against the official Codeforces editorial and problem page: https://codeforces.com/blog/entry/151310 and https://codeforces.com/contest/2205/problem/A. A third-party writeup with the same construction appears here: https://www.cnblogs.com/shojig/articles/19644510.
#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);
int pos = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
if (p[i] == n) pos = i;
}
swap(p[0], p[pos]);
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << p[i];
}
cout << '\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);
int pos = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
if (p[i] == n) pos = i;
}
swap(p[0], p[pos]);
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << p[i];
}
cout << '\n';
}
}