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.
Compare two neighboring windows of length . They share almost everything, so only one element leaves and one element enters. That’s where the blood is.
If both windows are valid, then their multiset difference must match:
Since is a permutation, the two values here are different.
For distinct values, equality of signed singleton multisets is brutally rigid: from
we must have and . So many positions of are forced to equal exactly.
Every position and every position is forced. The only positions that are not forced are the intersection of all length- windows:
This interval exists only when .
Outside , check that known equals . Inside it, only needs to be a permutation of the values . So known values there must be from this set and must not repeat. That’s the whole damn problem.
Look at two consecutive windows:
They differ only by removing and adding . The same must be true for the corresponding windows of .
Since both windows must match the corresponding windows as multisets, we get
Because is a permutation, . Therefore the signed multiset on the right has exactly one value and exactly one value. No funny business, no cancellation magic.
So we must have:
As ranges from to , this forces:
The only indices not covered by those forced ranges are
This is exactly the intersection of all length- windows. If , this interval is empty, so every position is forced and we only need wherever .
If , this middle interval exists. Let it be
Every length- window contains all positions of . Outside , we already know must equal . Therefore, inside , we only need the multiset of values to match:
Since is a permutation, the values for are all distinct. Thus the known non- values inside must satisfy two conditions:
The remaining positions can then be filled with the unused values from that same set. Clean. Almost annoyingly clean.
For each test case:
NO.YES.For every pair of consecutive windows, validity implies
Since , this forces
Therefore all indices in must equal .
The remaining interval is contained in every length- window. Once all outside positions match , every window matches if and only if the middle interval has the same multiset as in .
Our checks are exactly those necessary and sufficient conditions: outside values must match, and inside known values must form a subset without duplicates of the middle values of . The unfilled spots can be assigned the remaining middle values.
Therefore the algorithm answers YES exactly when a valid completion exists.
Each test case is processed in linear time:
The memory usage is also
Across all test cases, this is fine because
#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, k;
cin >> n >> k;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
int L = n - k + 1;
int R = k;
vector<char> allowed(n + 1, 0), seen(n + 1, 0);
for (int i = L; i <= R; i++) {
allowed[a[i]] = 1;
}
bool ok = true;
for (int i = 1; i <= n; i++) {
bool middle = (L <= i && i <= R);
if (!middle) {
if (b[i] != -1 && b[i] != a[i]) {
ok = false;
}
} else {
if (b[i] != -1) {
if (!allowed[b[i]] || seen[b[i]]) {
ok = false;
} else {
seen[b[i]] = 1;
}
}
}
}
cout << (ok ? "YES" : "NO") << '\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, k;
cin >> n >> k;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
int L = n - k + 1;
int R = k;
vector<char> allowed(n + 1, 0), seen(n + 1, 0);
for (int i = L; i <= R; i++) {
allowed[a[i]] = 1;
}
bool ok = true;
for (int i = 1; i <= n; i++) {
bool middle = (L <= i && i <= R);
if (!middle) {
if (b[i] != -1 && b[i] != a[i]) {
ok = false;
}
} else {
if (b[i] != -1) {
if (!allowed[b[i]] || seen[b[i]]) {
ok = false;
} else {
seen[b[i]] = 1;
}
}
}
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}