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 order of . Since you may reorder the final array, only the multiset of final values matters. Sorting is legal mental violence.
Think of the segment additions as horizontal layers. At height , every positive final position is covered by some operation-layer; at height , every value at least is still alive, and so on.
For any threshold , let be the number of elements of that are at least . These values describe the shape of the histogram of .
One very natural construction is: for every height , add one segment covering exactly the positions whose values are at least . After sorting , those positions are consecutive, so this really is a valid segment.
That construction creates segment lengths . Its maximum is , the number of positive elements. And no segment can be longer than that, because every position touched by the longest segment must end positive. So the answer is just the count of .
Key observation
The order of is a distraction. We only need the final multiset of values, because the statement lets us reorder the array at the end.
So imagine sorting the target values however we want. The cleanest choice is nonincreasing order:
Now the final array looks like a histogram. A segment addition is like placing one horizontal strip of height over some consecutive columns.
Upper bound
Suppose some operation uses a segment of length .
Every position inside that segment is incremented at least once, so every one of those final values is positive. Therefore:
So the answer can never exceed the number of positive elements in . That part is brutally simple: you cannot touch 7 final-zero positions and then pretend nothing happened. Math has receipts.
Let
We have shown:
Achieving the bound
Now sort in nonincreasing order. All positive values are in the prefix .
Since the input is guaranteed feasible, there exists some valid sequence of exactly segment additions that produces this sorted array. Look at the very first layer: every positive position must be incremented at least once, and no zero position can be incremented at all.
So the segment covering the first layer can be chosen as exactly:
That gives us an operation of length , which matches the upper bound.
Another way to see the same thing: for each height , the positions with form a prefix after sorting, so layers are naturally contiguous. The widest layer is height , and its width is exactly the number of positive values.
Final answer
The maximum possible segment length is:
So each test case is just counting positives. That's it. Rated 1200, but the core idea is basically: zeros are lava, positives are usable ground.
#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 ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x > 0) ans++;
}
cout << ans << '\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 ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x > 0) ans++;
}
cout << ans << '\n';
}
}