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.
Work with the required difference . The question is whether can be built as a sum of operation-vectors.
Kill the alternating signs by defining with 1-indexing. Then an operation becomes a constant range add: starts at even indices add , starts at odd indices add .
Careful: a rise in does not always mean a positive segment starts there. Ending a negative segment also creates a rise. That false shortcut dies pretty fast.
Think in terms of active intervals. Let be the number of active positive intervals and the number of active negative intervals. At position , we need .
Track only the maximum possible active positive count . At odd , positive intervals cannot start, so require . At even , the maximum active negative count is , so the new maximum positive count is .
Let . We need to represent the whole difference array as a sum of operation effects.
One operation on a segment contributes
starting from index .
Remove the alternating signs
Use 1-indexing and define
.
For an operation starting at , its contribution to is for every inside the segment. After multiplying by , its contribution to becomes
.
So in the transformed array :
Now the problem is: can we build array using positive intervals that may only start at even positions, and negative intervals that may only start at odd positions?
That is already much nicer.
The tempting wrong idea
You might think: if , then a positive interval must start at , so must be even. Nope. Ending a negative interval also increases the value. Similarly, ending a positive interval decreases the value.
For example, is possible: use a negative interval on and a positive interval on . The increase at index happens because the negative interval ended. So yeah, the naive adjacent-difference check gets cooked.
Active intervals
At any position, let:
Then the current value is
.
For a fixed , knowing determines :
.
So instead of tracking both counts, we can track possible values of .
The key fact is that after processing a position , all possible values of form an interval:
,
where is the maximum possible active positive count.
The lower bound is forced because and , so too.
Now we only need to update while scanning left to right.
Odd index transition
At an odd index, only negative intervals may start.
That means cannot increase. We can end some positive intervals, so can decrease, but it cannot go above the previous maximum .
To realize , we need some valid . This is possible iff
.
Equivalently, since , this only fails when .
If it works, the maximum possible stays the same:
.
Even index transition
At an even index, only positive intervals may start.
Now negative intervals cannot start; they can only continue or end. So the limiting resource is how many active negative intervals we could have had from the previous position.
At the previous position, the maximum possible positive count was , and the previous value was . Therefore the maximum possible negative count was
.
Call this maxNeg.
At index , choose any final negative count from to maxNeg. Then to get value , we need
.
So the maximum possible new positive count is
.
This transition is possible iff at least one valid nonnegative exists, i.e.
.
If not, answer is NO.
Why this is enough
The scan keeps exactly the useful information: how much positive interval capacity we can still have active while matching everything so far.
At odd indices, we can freely start negative intervals to lower the value, but cannot create new positive ones.
At even indices, we can freely start positive intervals to raise the value, but cannot create new negative ones.
Ending intervals is always flexible: any active interval can stop before the current position, and after the final position all remaining active intervals can end at . So there is no extra final condition.
Algorithm
For each test case:
prev = previous value;U = maximum possible active positive intervals.prev = 0, U = 0.newU = x_i + U - prev, and require newU >= max(0, x_i).YES; otherwise print NO.Complexity: per test case, memory for storing , and all counts fit in long long.
#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> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
bool ok = true;
ll prev = 0;
ll maxPos = 0;
for (int i = 1; i <= n; i++) {
ll b;
cin >> b;
ll d = b - a[i];
ll x = (i % 2 == 0 ? d : -d);
if (ok) {
if (i % 2 == 1) {
if (x > maxPos) ok = false;
} else {
ll maxNeg = maxPos - prev;
ll newMaxPos = x + maxNeg;
if (newMaxPos < max(0LL, x)) ok = false;
else maxPos = newMaxPos;
}
}
prev = 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<ll> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
bool ok = true;
ll prev = 0;
ll maxPos = 0;
for (int i = 1; i <= n; i++) {
ll b;
cin >> b;
ll d = b - a[i];
ll x = (i % 2 == 0 ? d : -d);
if (ok) {
if (i % 2 == 1) {
if (x > maxPos) ok = false;
} else {
ll maxNeg = maxPos - prev;
ll newMaxPos = x + maxNeg;
if (newMaxPos < max(0LL, x)) ok = false;
else maxPos = newMaxPos;
}
}
prev = x;
}
cout << (ok ? "YES" : "NO") << '\n';
}
}