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 exact operation order for a second and ask: what must be true right before you add some value to position ? Every earlier position must already be at least .
Look at the last operation applied to position . If the value right before that last operation is , then the chosen must satisfy , and the final value is .
Because and , the last added value must be more than half of : . So the earlier positions must all be at least some value strictly larger than .
The best possible last jump for position is the smallest integer bigger than , which is . Therefore every earlier final value must be at least .
So for every , check the minimum value among . You need If this holds everywhere, the array is reachable; otherwise it is dead on arrival.
The operation is annoying because it chooses the leftmost element below . So if we want to increment position , all earlier positions must already be at least .
That is the whole problem. Everything else is just making that sentence stop looking suspicious.
The Key Constraint
Suppose position gets incremented by value during some operation.
Right before that operation:
Now focus on the last operation ever applied to position .
Let the value of before that last operation be . After adding , it becomes the final target value:
But the operation requires:
So:
which means:
Since is an integer, the smallest possible value for this last jump is:
And because every earlier position must be at least before this last jump, every earlier final value must also be at least . Values only increase, so final values cannot be smaller than what they were at that moment.
Therefore, for every , we must have:
Equivalently:
If this fails for some , then position would need a final jump larger than all the previous values can support. No amount of clever operation ordering saves it. The leftmost rule is brutal like that.
Why The Condition Is Enough
Now we need to prove this condition is not just necessary, but also sufficient.
Assume for every :
We can build the target array from left to right.
For position , there are no earlier positions blocking us. Just choose , and becomes .
Now suppose we have already built positions through exactly. Let:
By the condition:
We want to make without touching earlier positions.
If , choose . All earlier positions are at least , so they are at least . Since , the operation hits position and sets it to .
If , use two operations:
The first operation hits position because , so all earlier positions are still at least that much.
After that, . The second operation with is valid for position because:
which is the same as:
This follows from:
So position can always be completed using either one operation or two operations, while earlier positions remain untouched.
That proves the condition is sufficient.
Algorithm
Scan the array left to right while maintaining:
For each from to , check:
If not, print NO. Otherwise keep scanning and update:
If all checks pass, print YES.
Complexity
Each test case is linear:
Total complexity over all tests is:
which is easily fine.
#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<ll> b(n);
for (ll &x : b) cin >> x;
ll prefMin = b[0];
bool ok = true;
for (int i = 1; i < n; i++) {
if (prefMin <= b[i] / 2) ok = false;
prefMin = min(prefMin, b[i]);
}
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<ll> b(n);
for (ll &x : b) cin >> x;
ll prefMin = b[0];
bool ok = true;
for (int i = 1; i < n; i++) {
if (prefMin <= b[i] / 2) ok = false;
prefMin = min(prefMin, b[i]);
}
cout << (ok ? "YES" : "NO") << '\n';
}
}