Editorial
Let be the number of 1s in the string, and let be the position of the -th 1 from the left.
For the shortest interval containing all 1s from the -th through the -th one to be usable for an operation, we need
Equivalently,
Define
Call an interval good if it satisfies the following recurrence:
For ,
The first condition corresponds to deleting the leftmost 1 first, while the second condition corresponds to deleting the rightmost 1 first.
Process in decreasing order and, for each fixed , process in increasing order. By maintaining the needed minimum and maximum incrementally, all values can be computed in time.
Lemma 1. A good interval with a 0 immediately outside its boundary can be reduced until only one 1 remains inside it.
We use induction on the interval length. Suppose the first condition of the recurrence holds. Choose the nearest satisfying . Then an operation interval satisfying the required balance can be chosen so that the leftmost 1 is deleted. The second condition is symmetric and allows deleting the rightmost 1. We then apply the induction hypothesis to the shorter good interval.
Lemma 2. An interval is good if and only if there exist such that
In other words, some occurrence of the maximum appears no later than some occurrence of the minimum. This follows by induction on the interval length using the recurrence defining good intervals.
Now let be the minimum number of good intervals into which the first 1s can be partitioned.
Once all good intervals are known, this DP also takes time.
It remains to prove that is exactly the answer.
Lemma 3. One operation cannot decrease the minimum number of good blocks in a partition.
Let the arrays before and after one operation be and . Deleting one 1 removes one element of the array, and every element to its right increases by .
Viewed in reverse, we obtain from by inserting a value at one position and then subtracting from every value in the suffix to its right. A good block not containing the insertion position has either all values unchanged or all values decreased by , so it remains good.
Let be the two endpoint 1s of the substring used for the deletion. Then . Therefore
so either or .
If , merge through the first block on the left containing a value at least . If , symmetrically merge through the first block on the right containing a value at most . By Lemma 2, the merged interval is good.
If the insertion position lies inside a block, the inequality together with Lemma 2 shows that a block that was good before the insertion is still good afterward. Hence the minimum good-partition count of is at most that of . Thus the minimum good-partition count never decreases as actual operations are performed.
Lemma 4. A minimum good partition of the initial array can actually be realized by operations.
Process the blocks of a minimum good partition from left to right. Since , there is a 0 to the left of the first block, so Lemma 1 applies.
After reducing one block to a single 1, suppose that this remaining 1 is adjacent to the first 1 of the next block. For two adjacent 1s, the value of the earlier one is exactly larger than the value of the later one. Therefore the remaining 1 and the next good block together also form a good interval. This would reduce the number of blocks in the minimum partition, a contradiction.
Therefore a 0 always exists immediately to the left of the next block, and Lemma 1 can be applied repeatedly until every block is reduced to one 1.
Suppose 1s remain after an arbitrary sequence of operations. The final array can be partitioned into singleton good intervals. Repeatedly applying Lemma 3 gives
Thus fewer than 1s can never remain. Conversely, Lemma 4 constructs a sequence leaving exactly 1s.
Therefore the answer is . If , the answer is .
The time complexity is per test case, and the space complexity is .
Solution written by GPT5.6