公式

C - Minimization of Divide 解説 by evima


Sort \(A\) and \(B\). Then, \(f(P)\) takes its minimum value when \(P=(1,2,\dots,N)\). For the proof, it suffices to show that if \(a < b\) and \(k > 0\), then \(a + \lfloor \frac{b}{2^k} \rfloor \le \lfloor \frac{a}{2^k} \rfloor + b\), which follows from the monotonic increase of \(x - \lfloor \frac{x}{2^k} \rfloor\).

Basically, as the form of the solution, there are almost only those that directly correspond \(A\) and \(B\), but we can rearrange \((a,b) = (3,0),(4,1)\) to \((a,b) = (3,1),(4,0)\), etc.

Specifically, consider which element of \(A\) to correspond to each \(0\) in \(B\). If \(B\) contains \(k\) zeros, we should basically correspond them to the smallest \(k\) elements of \(A\). However, we can rearrange the pair \((a,b) = (2k-1,0),(2k,1)\) to \((a,b)=(2k-1,1),(2k,0)\). After performing this rearrangement multiple times, delete all elements of \(A\) matched with \(0\), divide all remaining elements by \(2\), and recurse.

The state of \(A\) that appears in the middle has the form “a suffix of \(A\) has been divided by \(2^i\), and some copies of \(k\) in \(A\) have become \(k-1\)”. Furthermore, \(k\) takes either the minimum value of \(A\) or the second-smallest value. Here, we need to reduce the future cost by the number of times \(k\) became \(k-1\) (to cancel the cost of corresponding \(k\) with \(0\) in \(B\) in the previous step).

Moreover, it can also be seen that the value of pairs that can be rearranged when choosing the element of \(A\) to correspond to \(x\) in \(B\) is always constant. Based on this, we can perform transitions. As an example, we specifically explain the transition when \(k\) is odd and the pair that can be rearranged now is \((k,k+1)\). Let \(dp[i]\) be the number of cases where \(i\) copies of \(k\) have become \(k-1\), and let \(dp2[j]\) be the number of cases where \(j\) copies of the next \(\frac{k+1}{2}\) have become \(\frac{k+1}{2}-1\) due to rearrangement. Also, let \(x\) and \(y\) be the numbers of \(k\) and \(k+1\), respectively, \(z\) be the number of \(0\)s in \(B\), and \(w = x+y-z\).

Noting that those where \(k\) has become \(k-1\) cannot be rearranged, the transition is as follows:

  • \(dp2[j] = \sum_{i}^{} dp[i] \binom{x-i}{j} \binom{y}{w-j}\)

There are also cases where \((x,x+1)\) can be rearranged for \(k+1 < x\), \((k-1,k)\) can be rearranged, and \((k-2,k-1)\) can be rearranged, but all have similar transitions. If we then speed up this transition with convolution, the time complexity becomes \(\mathrm{O}(B N \log N)\), which is sufficiently fast.

投稿日時:
最終更新: