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.
Forget the numeric values for a second. Since is a permutation, every value has one original position .
When a value gets copied, what really gets copied is its original position label. So the final array corresponds to the sequence .
Initially, those origin labels are exactly from left to right. Try to find a property of this sequence that copying one adjacent label over the other can never break.
The key invariant is monotonicity: if origin labels are nondecreasing, replacing one of two adjacent labels by the other keeps them nondecreasing. So any decrease in is instantly dead.
That invariant is also sufficient: every nondecreasing origin-label sequence can be generated. So the whole solution is just checking whether .
Research check: the official Codeforces Round 1079 editorial page for 2197B includes solution code that stores each value's position in and rejects exactly when these positions decrease while scanning . Public OK submissions are listed for the same problem on cf-problemset. The supplied statement is still canonical.
Let be the index where value appears in the original permutation . Since is a permutation, this is well-defined for every .
Instead of tracking the values themselves, track their origin labels. The value has origin label . Initially, the origin-label array is
If the final array is , then its origin-label sequence must be
So the question becomes: starting from , which arrays of origin labels can be made by repeatedly copying one adjacent label into the other?
The answer is exactly the nondecreasing ones.
First, necessity. Initially the origin labels are nondecreasing. Suppose adjacent labels are . Copying left to right makes the pair ; copying right to left makes it . Either way, no inversion appears. So if for some ,
then the target is impossible. No amount of adjacent-copy button mashing fixes that.
Now sufficiency. We prove by induction that every nondecreasing sequence with values in can be generated from .
For , trivial. For larger , every nondecreasing map has a fixed point: some with . Take the smallest such that . This exists because . If , then . Otherwise, , so ; monotonicity gives , and together with , we get .
Because is nondecreasing and , the prefix only needs labels from , and the suffix only needs labels from . By induction, build the prefix using only that prefix. Then position is label , and positions after are untouched. Shift the suffix labels by and apply induction again on positions . The two constructions only overlap at , whose final label is still .
So the invariant is not merely necessary; it is the full characterization.
Implementation:
NO; otherwise print YES.Do not assume a value's original position must lie inside its final block. Sample 6 kills that false idea: value starts at position but ends at positions , and the answer is YES.
Complexity is per test case and memory. Across all tests, this 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<int> pos(n + 1);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
pos[x] = i;
}
bool ok = true;
int last = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (pos[x] < last) ok = false;
last = pos[x];
}
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 main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> pos(n + 1);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
pos[x] = i;
}
bool ok = true;
int last = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (pos[x] < last) ok = false;
last = pos[x];
}
cout << (ok ? "YES" : "NO") << '\n';
}
}