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.
Look at the operation as moving values on positions, not as changing the values themselves. Position is directly connected only to position .
If swaps are allowed along edges of a graph, then within one connected component you can reorder the values however you want using adjacent swaps along paths.
The graph here has edges . What do its connected components look like? Try writing the positions for : , then , then , then , .
A position belongs to the chain determined by its odd part: repeatedly divide by until the number becomes odd. For example, , so position is in component .
In the sorted array, value must end up at position . So every current value must be in the same component as position , i.e. .
Research note: the Codeforces statement is canonical. The official round editorial currently shows code for 2195B instead of a full written explanation; that code repeatedly swaps along chains and then checks whether the array became sorted. Arpa's video-tutorial post links an accepted submission for this problem, and a public solution pattern checks whether position and value differ by a power-of-two factor, which is the same as checking equal odd parts.
Build a graph whose vertices are positions . For every valid operation, there is an edge between and . Swapping along an edge means a value can move across that edge. Since we can repeat operations, a value can move anywhere inside its connected component.
Now inspect the shape of this graph. Every position is connected to , , and so on while those positions exist. It is also connected upward to when is even. Therefore, if you keep dividing a position by until it becomes odd, that final odd number identifies its whole component.
Define
Two positions and are in the same connected component exactly when
For example, the component with odd root is
up to .
The target sorted array is fixed: value must end at position . Since values can never leave their connected component, value currently sitting at position can reach its final position only if positions and are in the same component. So a necessary condition is
for every .
This condition is also sufficient. If every value belongs to the same component as its target position, then inside each component the set of current values is exactly the same as the set of positions in that component. A connected graph lets us realize any permutation inside the component using swaps along paths. In this problem the components are just chains, so this is even more direct: adjacent swaps along a chain can bubble any value to any place in that chain. No heap magic, just graph components wearing a fake mustache.
So the algorithm is simple:
NO.YES.Edge cases:
Complexity: computing an odd part by repeated division takes , so the simple implementation is per test case. Since , that is easily fine. Memory usage is besides input storage, and we can process the values as they are read.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int odd_part(int x) {
while (x % 2 == 0) x /= 2;
return x;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
bool ok = true;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (odd_part(i) != odd_part(x)) ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int odd_part(int x) {
while (x % 2 == 0) x /= 2;
return x;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
bool ok = true;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (odd_part(i) != odd_part(x)) ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
}
}