B - Electric Board Editorial by evima
The operation does not change the number of 0
s, so if and have different number of 0
s, the objective is unachievable.
Otherwise, assume that the 0
s in are its -th, -th, , -th characters, and the 0
s in are its -th, -th, , -th characters. We will show that the answer is equal to the number of indices such that .
First, we need at least this number of operations because one operation changes only one of .
(Illustration of the operation)
Additionally, we can achieve the objective in this number of operations with, for example, the following strategy:
If there exists such that : Let be the minimum such that . Use the operation to change to .
Otherwise: Let be the maximum such that . Use the operation to change to .
(Illustration of the strategy)
(The sentences to the left says “The minimum such that is ”, “The maximum such that is ”, and the one to the right says “We have one less such that !”)
(Sample Implementations)
posted:
last update: