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 whole array for a second. Classify which single triples can satisfy .
If a triple has no , then its MEX is . So the right side must also be , meaning the triple must have .
If a triple contains , then its MEX is positive. But then , so the right side is just . That would require , which is impossible because is present in the triple while MEX is missing.
So every valid triple is exactly three equal positive numbers: with . No spicy exceptions hiding in the corner.
Since every adjacent triple must be equal positive, the whole completed array must be one repeated positive value. Therefore the given non-missing values must all be equal, and that common value must not be .
The key is to stop trying to “fill” the array first. The condition is local, so we should first understand what a valid length- block can even look like.
Take any triple .
Let
We need:
Classifying one triple
There are two cases.
Case 1: the triple does not contain .
Then the smallest missing non-negative integer is , so:
So we need:
That means , so all three values are equal. Also, since the triple does not contain , that equal value must be positive.
So triples like , , work.
Case 2: the triple contains .
Then the MEX is positive, because is already present.
Also, since all values are non-negative and is present, we have:
So the right side becomes:
The condition would require:
But is literally one of the values in the triple. The MEX is supposed to be a value that is missing. So this is impossible. That assumption crashes immediately. Beautifully rude math.
Therefore, the only valid triples are:
What this means for the whole array
Every consecutive triple must be valid. So every consecutive triple must contain three equal positive values.
That forces the whole completed array to be one constant positive value.
For example, from the triple we get:
From we get:
So equals the same value too. Continuing this, every position must equal the same positive number.
So the problem becomes very simple:
Can all known values already fit into one positive constant array?
That is true exactly when:
If every element is , we can choose any positive value, like , for all positions.
If the known values are all , fill every missing spot with .
If there is a , impossible.
If there are two different positive known values, impossible.
So for each test case, scan the array and keep the first known value. Every other known value must match it, and it must be positive.
#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;
int value = -1;
bool ok = true;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == -1) continue;
if (x == 0) ok = false;
else if (value == -1) value = x;
else if (value != 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 main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int value = -1;
bool ok = true;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == -1) continue;
if (x == 0) ok = false;
else if (value == -1) value = x;
else if (value != x) ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
}
}