公式

D - Magnets 解説 by evima


This problem can be restated as follows:

Is it possible, by performing the operation below any number of times, to transform \(A\) into \(B\)? If yes, find the minimum number of operations needed.

  • Pad \(A\) with one 0 on the left and right, then replace any consecutive block of length \(3\) by the maximum value in that block (that is, if the block contains 1, replace it with 1; otherwise 0).

To solve this, first consider the decision problem \(P(x)\): “Is it possible to transform \(A\) into \(B\) with exactly \(x\) operations?” This is equivalent to:

Can the string \(A^{(x)} \coloneqq \)0\(^x A\)0\(^x\), obtained by padding \(A\) with \(x\) zeros on each side, be split into \(N\) consecutive odd-length substrings, each of which is replaced by its maximum value, to form \(B\)?

For \(P(x)\) to be true, at minimum the number of leading and trailing zeros in \(A^{(x)}\) must be at least as large as those of \(B\). That is, for a string \(s\), let \(L(s)\) and \(R(s)\) denote the number of consecutive zeros at the start and end of \(s\), and we need:

\[L(A) +x = L(A^{(x)}) \geq L(B), \quad R(A) + x = R(A^{(x)}) \geq R(B).\]

This yields a necessary condition for \(P(x) = \mathrm{true}\):

\[x \geq m \coloneqq \max\{L(T)-L(S), R(T)-R(S)\}. \tag{1}\]

Below, we assume (1).

We can determine \(P(x)\) by a greedy algorithm that constructs \(B\) from left to right, cutting out corresponding prefixes of \(A^{(x)}\). Concretely, initialize \(A' \gets A^{(x)}\), run-length encode the 0s in \(B\), and do as follows:

  • To construct a 1 in \(B\), cut out the shortest prefix of \(A'\) that contains a 1. However, when constructing the last 1 in \(B\), we must use a prefix containing all the remaining 1s of \(A'\), so we choose the shortest odd-length prefix containing the last 1.
  • To construct 0\(^k\) in \(B\), we need to cut out 0 \(k\) times from the beginning of \(A'\). This requires \(A'\) to have 0\(^k\) as a prefix. If not, we repeatedly “fix” it by repeatedly extending the length of the substring used for the most recent 1 in \(B\) by two characters and discarding those two characters from the beginning of \(A'\) until 0\(^k\) appears as a prefix. (Because of (1), whenever this fix is needed, there was actually an operation to construct the most recent 1 in \(B\).)

This process finds \(P(x)\) for a given \(x\) in \(O(N)\) time. (If and only if we cannot cut out substrings to match \(B\) by this process, \(P(x) = \mathrm{false}\).)

On the other hand,

\[\text{if} \,\, x \geq m+2 \,\,\text{and}\,\, P(x) = \mathrm{true, then}\,\, P(x-2) = \mathrm{true} \tag{2}.\]

To see this, note that if \(x \geq m+2\) and \(P(x) = \mathrm{true}\), the greedy algorithm decomposes \(A^{(x)}\) into \(N\) substrings using some \(k, k', l \geq 0\) and strings \(s_1, \ldots, s_l\) as follows:

\[A^{(x)} = \underbrace{0|0|\ldots|0}_{L(B)}|0^{k+2}s_1|s_2|s_3|\ldots|s_l|\underbrace{0|0|\ldots|0}_{R(B)-1}|0^{k'+3}.\]

From this, we can form a decomposition of \(A^{(x-2)}\) as

\[A^{(x-2)} = \underbrace{0|0|\ldots|0}_{L(B)}|0^{k}s_1|s_2|s_3|\ldots|s_l|\underbrace{0|0|\ldots|0}_{R(B)-1}|0^{k'+1},\]

showing \(P(x-2) = \mathrm{true}\).

From (1) and (2), it suffices to check only \(P(m)\) and \(P(m+1)\) to solve the original problem. Thus, we can solve the problem in \(O(N)\) time overall.

投稿日時:
最終更新: