C - AB*A Changing Editorial by evima
Convert each of \(S\) and \(T\) into the following non-negative integer less than \(2^N\):
- The non-negative integer whose \(2^{i-1}\) bit is \(0\) if the \(i\)-th character of the string is
A, and \(1\) if it isB.
Then, the operation on \(S\) can be considered as adding \(3 \times 2^i\) to \(S\) for some \(i\) such that the \(2^i\) bit of \(S\) is \(0\).
Consider for a sequence of non-negative integers \((x_0, x_1, \ldots, x_{N-1})\) whether we can perform \(x_i\) operations on \(2^i\) to make \(S = T\). Naturally, we need \(\sum 2^i x_i = \frac{T - S}{3}\). First, we can perform one operation on \(2^i\) if the \(2^i\) bit of \(S\) is \(0\); moreover, if operations on lower bits change the \(2^i\) bit, we can perform the operation again, and this happens at most \(\left\lfloor \frac{S + \sum_{j=0}^{i-1} 3 \times 2^j x_j}{2^i} \right\rfloor - \left\lfloor \frac{S}{2^i} \right\rfloor\) times. Therefore, \(x_i\) must be less than or equal to the sum of these.
Now, if we always choose for the operation the largest \(i\) among those where the \(2^i\) bit is \(0\) and we still need to perform operations on \(2^i\), we can show in ascending order of \(i\) that the above condition is also sufficient.
We define \(x_0, \ldots, x_{N-1}\) using the following procedure. Let \(y_i\) be the maximum number of times we can perform operations on \(2^i\), and initially set \(y_i\) to the \(2^i\) bit of \(\frac{T - S}{3}\). For \(i = N - 1, \ldots, 0\) in this order, set \(x_i\) to satisfy \(x_i \leq \left\lfloor \frac{S + \sum_{j=0}^{i-1} 3 \times 2^j x_j}{2^i} \right\rfloor - \left\lfloor \frac{S}{2^i} \right\rfloor\), and increase \(y_{i-1}\) by \(2(y_i - x_i)\). If we consider \(y_{-1}\) for convenience and find that \(y_{-1} = 0\), then we have obtained valid \((x_0, \ldots, x_{N-1})\).
We show that the greedy strategy of maximizing \(x_i\) at each \(i\) in the above procedure is optimal. Suppose we have a non-greedy strategy resulting in \(x_i', y_i'\). Let \(k\) be the last position where we could have increased \(x_k\) by \(1\) but didn’t. Suppose that if we increase \(x_k\) by \(1\) and follow the greedy strategy thereafter, we get \(x_i'', y_i''\). The change in strategy results in \(y_{k-1}' \lt y_{k-1}''\), but if we adopt the greedy strategy from then on, we have \(y_{k-2}' \leq y_{k-2}'', y_{k-3}' \leq y_{k-3}'', \ldots, y_{-1}' \leq y_{-1}''\). Thus, when replacing a non-greedy strategy with a greedy one, the validity of \((x_0, \ldots, x_{N-1})\) is maintained. Also, we have \(x_{k-1}' \leq x_{k-1}'', \ldots, x_0' \leq x_0''\). Although \(x_k\) increases by \(1\), there must exist some \(k' (0 \leq k' < k)\) such that \(x_{k'}' > x_{k'}''\) (otherwise the sum of \(2^i x_i\) wouldn’t match), so \(\sum x_i\) does not increase. Therefore, if a non-greedy strategy is optimal, replacing it with a greedy one still yields an optimal solution, justifying the greedy approach.
All that’s left is to simulate the above procedure. By tracking the behavior, we can confirm that \(y_i\) is always at most \(3\), and the highest bits of the values we manage are only a few digits above \(i\). Therefore, additions and subtractions can be done in \(\mathrm{O}(1)\) time, allowing us to solve the problem in overall \(\mathrm{O}(N)\) time.
posted:
last update: