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

Dadas and Mog's Binary Sequence

Back to problem

Editorial

For a binary string CCC, consider the values H0(C),H1(C),…,HN(C)H_0(C),H_1(C),\ldots,H_N(C)H0​(C),H1​(C),…,HN​(C) defined in the constraints. Reading one character changes the value by exactly one.

For each iii (1≤i≤N1 \leq i \leq N1≤i≤N), define

Ei(C)=min⁡(Hi−1(C),Hi(C)).E_i(C)=\min(H_{i-1}(C),H_i(C)).Ei​(C)=min(Hi−1​(C

The equality Ei(C)=xE_i(C)=xEi​(C)=x means that the iii-th step crosses the boundary between the integers xxx and x+1x+1.

An interval [l,r][l,r][l,r] contains equally many zeroes and ones exactly when Hl−1(C)=Hr(C)H_{l-1}(C)=H_r(C)Hl−1​(C)=H. Applying an operation reverses only the order of the crossed boundaries inside this interval. Thus, it reverses and leaves all other values unchanged.

Therefore, for every integer xxx, the number of crossings between xxx and x+1x+1x+1 is invariant. Hence the following two multisets must be equal:

{E1(A),E2(A),…,EN(A)},{E1(B),E2(B),…,EN(B)}.\{E_1(A),E_2(A),\ldots,E_N(A)\},\qquad \{E_1(B),E_2(B),\ldots,E_N(B)\}.{E1​(A),E

This condition is also sufficient. Regard E(A)E(A)E(A) as the current sequence and E(B)E(B)E(B) as the target sequence, and make them equal from left to right.

Assume the first i−1i-1i−1 positions already match. If Ei(A)=Ei(B)E_i(A)=E_i(B)Ei​(A)=Ei​, continue. Otherwise, the two edge levels describe the two opposite directions available from the current height. Since the current path first moves in the wrong direction, the first later crossing of the target edge leaves the current height toward the target side, and its second crossing returns to the current height.

Let jjj be the position of the second occurrence of Ei(B)E_i(B)Ei​(B) in the current suffix starting at iii. The original binary-string interval [i,j][i,j][i, starts and ends at the same height, so it contains equally many zeroes and ones and is a valid operation interval.

The operation reverses Ei(A),Ei+1(A),…,Ej(A)E_i(A),E_{i+1}(A),\ldots,E_j(A)Ei​(A),Ei+1​(A),…,. Since the last value is , the new value at position becomes correct.

When the invariant multisets are equal, the required second occurrence always exists. For every position, scan the remaining suffix linearly and reverse the selected segment directly. Each position costs O(N)O(N)O(N) time, so the total time complexity is O(N2)O(N^2)O(N2). The memory complexity is O(N)O(N)O(N).

At most one operation is produced for each position, so the total number of operations is at most NNN.

Solution written by GPT5.6

)
,
Hi​
(
C
))
.
x
+
1
r
​
(
C
)
El(C),El+1(C),…,Er(C)E_l(C),E_{l+1}(C),\ldots,E_r(C)El​(C),El+1​(C),…,Er​(C)
2
​
(
A
)
,
…
,
EN​
(
A
)}
,
{
E1​
(
B
)
,
E2​
(
B
)
,
…
,
EN​
(
B
)}
.
(
B
)
j
]
Ej​
(
A
)
Ej(A)=Ei(B)E_j(A)=E_i(B)Ej​(A)=Ei​(B)
iii