DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Binary String Reduction

Back to problem

Editorial

Let KKK be the number of 1s in the string, and let xix_ixi​ be the position of the iii-th 1 from the left.

For the shortest interval containing all 1s from the aaa-th through the bbb-th one to be usable for an operation, we need

xb−xa+1≤2(b−a+1)−1.x_b-x_a+1 \le 2(b-a+1)-1.xb​−xa​+1≤2(b−

Equivalently,

xa−2a≥xb−2b.x_a-2a \ge x_b-2b.xa​−2a≥xb​−2b.

Define

qi=xi−2i.q_i=x_i-2i.qi​=xi​−2i.

Call an interval [l,r][l,r][l,r] good if it satisfies the following recurrence:

g[l][l]=true.g[l][l]=\mathrm{true}.g[l][l]=true.

For l<rl<rl<r,

g[l][r]=(g[l+1][r]∧ql≥min⁡(ql+1,…,qr))∨(g[l][r−1]∧max⁡(ql,…,qr−1)≥qr).g[l][r] = \left(g[l+1][r] \land q_l \ge \min(q_{l+1},\ldots,q_r)\right) \lor \left(g[l][r-1] \land \max(q_l,\ldots,q_{r-1}) \ge q_r\right).g[l][r]=(

The first condition corresponds to deleting the leftmost 1 first, while the second condition corresponds to deleting the rightmost 1 first.

Process lll in decreasing order and, for each fixed lll, process rrr in increasing order. By maintaining the needed minimum and maximum incrementally, all values g[l][r]g[l][r]g[l][r] can be computed in O(K2)O(K^2) 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 iii satisfying ql≥qiq_l \ge q_iql​≥qi​. 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 [a,b][a,b][a,b] is good if and only if there exist a≤i≤j≤ba \le i \le j \le ba≤i≤j≤b such that

qi=max⁡(qa,…,qb),qj=min⁡(qa,…,qb).q_i=\max(q_a,\ldots,q_b), \qquad q_j=\min(q_a,\ldots,q_b).qi​=max(qa​,

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 dp[i]dp[i]dp[i] be the minimum number of good intervals into which the first iii 1s can be partitioned.

dp[0]=0,dp[0]=0,dp[0]=0, dp[r]=min⁡1≤l≤r, g[l][r](dp[l−1]+1).dp[r]=\min_{1 \le l \le r,\ g[l][r]}\left(dp[l-1]+1\right).dp[r]=1≤l≤r, g[l][r]

Once all good intervals are known, this DP also takes O(K2)O(K^2)O(K2) time.

It remains to prove that dp[K]dp[K]dp[K] is exactly the answer.

Lemma 3. One operation cannot decrease the minimum number of good blocks in a partition.

Let the qqq arrays before and after one operation be QQQ and Q′Q'Q′. Deleting one 1 removes one element of the qqq array, and every element to its right increases by 222.

Viewed in reverse, we obtain QQQ from Q′Q'Q′ by inserting a value zzz at one position and then subtracting 222 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 222, so it remains good.

Let a,ba,ba,b be the two endpoint 1s of the substring used for the deletion. Then qa≥qbq_a \ge q_bqa​≥qb​. Therefore

(qa−z)+(z−qb)=qa−qb≥0,(q_a-z)+(z-q_b)=q_a-q_b \ge 0,(qa​−z)+(z−q

so either qa≥zq_a \ge zqa​≥z or z≥qbz \ge q_bz≥qb​.

If qa≥zq_a \ge zqa​≥z, merge through the first block on the left containing a value at least zzz. If z≥qbz \ge q_bz≥q, 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 qi+1−qi≥−1q_{i+1}-q_i \ge -1qi+1​−qi​≥−1 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 QQ 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 qqq array can actually be realized by operations.

Process the blocks of a minimum good partition from left to right. Since S1=0S_1=\texttt{0}S1​=0, 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 qqq value of the earlier one is exactly 111 larger than the qqq 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 mmm 1s remain after an arbitrary sequence of operations. The final array can be partitioned into mmm singleton good intervals. Repeatedly applying Lemma 3 gives

dp[K]≤m.dp[K] \le m.dp[K]≤m.

Thus fewer than dp[K]dp[K]dp[K] 1s can never remain. Conversely, Lemma 4 constructs a sequence leaving exactly dp[K]dp[K]dp[K] 1s.

Therefore the answer is dp[K]dp[K]dp[K]. If K=0K=0K=0, the answer is 000.

The time complexity is O(N2)O(N^2)O(N2) per test case, and the space complexity is O(N2)O(N^2)O(N2).

Solution written by GPT5.6

a+
1)−
1.
g
[
l
+
1
]
[
r
]
∧
ql​
≥
min
(
ql+1​
,
…
,
qr​
)
)
∨
(g[l][r−1]∧max(ql​,…,qr−1​)≥qr​).
O
(
K2
)
…
,
qb​
)
,
qj​
=
min(qa​,…,qb​).
min
​
(dp[l−1]+1)
.
b
​
)
=
qa​−
qb​≥
0,
b
​
zzz
Q
Q′Q'Q′