Official

C - Power Up Editorial by evima


From now on, let operation \(x\) be defined as transforming two instances of \(x\) into one instance of \(x+1\). Let \(B_x\) denote the count of \(x\) in \(A\). Note that the integers in \(A\) can be at most \(\max A + \log N\).

If you perform both operation \(x\) and operation \(y\) (where \(x < y\)), we may assume that you perform operation \(x\) first. This is because performing operation \(x\) may enable operation \(y\), but the reverse does not happen.

Therefore, we can proceed with dynamic programming as follows:

  • \(\mathrm{dp}[i][j] =\) The number of possible states of \(A\) when all operations \(k(k\le i)\) have been performed, and now there are \(j\) instances of \(i+1\).

By performing operation \(i+1\) \(k\) times and adding the original \(B_{i+1}\), we can transition from \(\mathrm{dp}[i][j](j \ge 2k)\) to \(\mathrm{dp}[i+1][k+B_{i+1}]\).

Let’s evaluate the computational complexity. \(i\) can be limited by the maximum integer that can be contained in \(A\), which is \(\mathrm{O}(\log N + \max A)\). \(j\) can be limited by \(|A|\), which is \(\mathrm{O}(N)\). Therefore, by accelerating the transition with cumulative sums, this problem can be solved in \(\mathrm{O}(N(\log N + \max A))\).

However, this is not fast enough. Here, we pay attention to the length of \(\mathrm{dp}[i]\). The total sum of \(2^j\) for the elements \(j\) in \(A\) that are at most \(i\) is decreased or unchanged by the operations. Thus, we have (the maximum count of \(i\) during the operations) \(\leq \frac{\sum_{j=1}^{i} B_j 2^j}{2^i}\).

Thus,

\(\sum_{i=1}^{\max A + \log N}\) (the maximum count of \(i\) in \(A\) during the operations) \(\le \sum_{i=1}^{\max A + \log N} \frac{\sum_{j=1}^{i} B_j 2^j}{2^i}\)

\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{i=1}^{\max A + \log N} \sum_{j=1}^{i} B_j\left(\frac{1}{2}\right)^{i-j} \)

\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{j=1}^{\max A + \log N} \sum_{i=j}^{\max A + \log N} B_j\left(\frac{1}{2}\right)^{i-j} \)

\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \le\sum_{j=1}^{\max A + \log N} 2B_j\)

\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \le 2N\).

Therefore, considering \(\mathrm{dp}[i][0]\) as well, we were able to evaluate the total length of \(\mathrm{dp}[i]\) as \(\mathrm{O}(N + \max A)\). As a result, by accelerating the previously mentioned dynamic programming with cumulative sums, we can solve this problem in \(\mathrm{O}(N + \max A)\).

posted:
last update: