公式

E - Delete AB 解説 by evima


Let \(N = |S|\), replace A with \(+1\) and B with \(-1\), and take the cumulative sum to get \(h = (h_0, h_1, \dots, h_N)~(h_0 = 0)\).
Let \(id = (id_0, id_1, \dots, id_k)\) be the sequence of indices remaining in \(h\). (We set \(k =|h|-1\).)

The operation in the problem can be restated as: choose a position with three consecutive terms \((h_a, h_b, h_c)\) in \(h\) where \(h_a = h_c = h_b-1\), and delete \(h_a, h_b\) or \(h_b, h_c\).
A string being a palindrome means that \(k+1\) points \((0, h_{id_0}),(1, h_{id_1}), \dots, (k , h_{id_k})\) on the two-dimensional plane are point-symmetric.
Consider the elements in (the final) \(h, id\) for the string to be a palindrome.

First, \(h_{id_0} = 0\) and \(h_{id_k} = h_N\) always hold regardless of operations.
Therefore, the center of point symmetry is \(\displaystyle \left(\frac{k}{2}, \frac{h_0 + h_N}{2}\right)\). So if the point corresponding to \((i, h_{id_i})\) is \((j, h_{id_j})\), then \(h_{id_i} + h_{id_j} = h_0 + h_N\) must hold.

At least one \(h_x\) such that \(h_x = \min(h)\) must be included in \(h\). In this case, \(h_y\) such that \(h_x + h_y = h_0 + h_N\) also needs to be included in \(h\).
If no such \(h_y\) exists, a palindrome cannot be obtained.

Fix one pair of \(x, y\) in \(id\). (Without loss of generality, assume \(x<y\).)
By appropriately deleting elements other than \(h_x, h_y\) from \((h_x, h_{x+1}, \dots, h_y)\), we can make it \((h_x, h_x+1, h_x+2, \dots, h_y-1, h_y)\).

Next, consider performing operations on \((h_y, h_{y+1}, \dots, h_N)\) while keeping \(h_y\). If \(\displaystyle h_p = \min_{y \leq i \leq N}(h_i)\), then \(h_p\) must be included in \(h\), and \(h_q~(0 \leq q \leq x)\) such that \(h_p + h_q = h_0 + h_N\) also needs to be included in \(h\).
If no such \(h_q\) exists, a palindrome cannot be obtained.

Repeat updating \((x, y) \leftarrow (p,q)\) by choosing one \((p, q)\), and if this can be continued to both ends, one palindrome can be obtained.

For the choice when there are multiple possible \(x,y\) (and \(p,q\)), \(y,q\) should be chosen as far from the center of the palindrome as possible, and \(x, p\) can be chosen arbitrarily. (Proof later)
However, only for the initial \(x, y\), we need to try both \(x < y\) and \(x > y\).

Here, the number of updates to \((x, y)\) is \(O(\sqrt {|S|})\).
This follows from the fact that the worst case is when \(h_{\min(x, y)}\) is updated in the order \(\dots \rightarrow -4 \rightarrow 4 \rightarrow -3 \rightarrow 3 \rightarrow -2 \rightarrow 2 \rightarrow -1 \rightarrow 1 \rightarrow 0\).

We said that \(x, p\) can be chosen arbitrarily, but if we choose the one farthest from the palindrome center like \(y, q\), it can be processed with Mo’s Algorithm.

By using Sparse Table or similar for minimum value retrieval, this can be solved in \(O(N\sqrt Q + Q\sqrt N + N\log N)\) time.

Proofs

Let \((a → b)\) denote \((a, a+1, \dots, b)\) if \(a < b\), and \((a, a-1, \dots, b)\) if \(a > b\).

  • Proof Item \(1\): When \(y~(h_y = h_N - \min(h))\) is fixed, if there exist \(x_1, x_2\) such that \(h_{x_1} = h_{x_2} = \min(h)\) and \(x_1 < x_2 < y\), it is better to keep \(x_2\) rather than \(x_1\).
    • It can be similarly shown that when \(y < x_1 < x_2\), it is better to keep \(x_1\).

Suppose that in the state where \(x_1 \in id\), the center of the palindrome is between \(x_1\) and \(y\) (inclusive), and we can make a palindrome by appropriately deleting elements other than \(h_{x_1}\) and \(h_{y}\) from \((h_0, \dots, h_{x_1})\) and \((h_{y}, \dots, h_N)\).
Then, \(\displaystyle \min_{x_1 \leq i \leq x_2}(h_i) = h_{x_1} = h_{x_2}\), so we can delete \(h_{x_1}, \dots, h_{x_2-1}\) in the state where \(x_2 \in id\), and this always allows us to obtain the same palindrome as before.

(End of Proof Item \(1\))

  • Proof Item \(2\): When \(x~(h_x = \min(h))\) is fixed, if there exist \(y_1, y_2\) such that \(h_{y_1} = h_{y_2} = h_N - h_x\) and \(x < y_1 < y_2\), it is better to keep \(y_2\) rather than \(y_1\).
    • It can be similarly shown that when \(y_1 < y_2 < x\), it is better to keep \(y_1\).

Let \(\displaystyle p = \argmin_{y_1 \leq i \leq y_2}(h_i), q = \argmin_{y_2 \leq i \leq N}(h_i)\).

Suppose we can make a shortest palindrome by deleting elements other than \(h_x, h_{y_1}\) from \((h_0, \dots, h_{x})\) and \((h_{y_1}, \dots, h_N)\).

When \(h_p > h_q\)

\(h_q\) must be included in \(h\), and due to minimum length, the palindrome has \((h_x → h_{y_1} → h_q)\) near the center.

In this case, we can make \((h_x, h_{x+1}, \dots, h_{y_2})\) and \((h_{y_2}, h_{y_2+1}, \dots, h_q)\) into \((h_x → h_{y_2})\) and \((h_{y_2} → h_q)\), respectively, so the same palindrome is obtained.


When \(h_p \leq h_q\)

\(h_p\) will be included in \(h\). Let the location corresponding to \(h_p\) in the palindrome be \(h_{p'}\).

When it becomes a palindrome, it has the form \((0, \dots, h_{p'} → h_x → h_{y_1} → h_{p}, \dots, last (= h_N))\). Here, \(h_p < last\), so \(h_{p'} > 0\).
Since the difference between adjacent terms of \(h\) is \(1\), there exist elements with values equal to \(h_N- h_q\) in the prefix and \(h_q\) in the suffix, and if we denote them (the farthest from the palindrome center) as \(h_l\) and \(h_r\), respectively, we can represent it as \((0, \dots, h_l, \dots, h_{p'} → h_x → h_{y_1} → h_{p}, \dots, h_r, \dots, last)\).

When \(y_2 < r\), by Proof Item \(1\), \(q \leq r\), so we can obtain a palindrome that can be written as \((0, \dots, h_l → h_x → h_{y_2} → h_q, \dots, last)\).

When \(r < y_2\), \(\displaystyle h_r = \min_{r \leq i \leq N}(h_i)\) holds, so we can delete elements from \((h_r, h_{r+1}, \dots, h_q)\) to obtain \((h_r)\), but we may obtain \((h_q)\) instead, and the rest is the same as when \(y_2 < r\).

(End of Proof Item \(2\))

  • Proof Item \(3\): When \(y~(h_y = h_N - \min(h))\) is fixed, if there exist \(x_1, x_2\) such that \(h_{x_1} = h_{x_2} = \min(h)\) and \(x_1 < x_2 < y\), either \(x_1\) or \(x_2\) may be kept.
    • It can be similarly shown that when \(y < x_1 < x_2\), either \(x_1\) or \(x_2\) may be kept.

Suppose we keep \(x_2\).

If we deleted all of \(h_{x_1+1}, h_{x_1+2}, \dots, h_{x_2-1}\), it does not matter if we keep \(x_1\) instead of \(x_2\).

Suppose we kept \(h_p~(> h_{x_1})\) among \(h_{x_1+1}, h_{x_1+2}, \dots, h_{x_2-1}\). In this case, \(h_{x_1}\) cannot be deleted, but by Proof Item \(2\), there is no point corresponding to \(h_{x_1}\).

Combined with Proof Item \(1\), either \(x_1\) or \(x_2\) may be kept.

(End of Proof Item \(3\))

投稿日時:
最終更新: