公式

E - Hierarchical Majority Vote 解説 by en_translator


This problem can be solved with Dynamic Programming (DP).

Let \(A^{(k)}=(A_1^{(k)},\dots,A_{3^{N-k}}^{(k)})\) be the \(k\)-time repetition of \(A\). \(A^{(k)}\) can be obtained by naively preforming the operation int the problem statement. Let \(f(k,i)\) be the number of elements in \(A\) required to alter, in order to flip the value \(A^{(k)}_i \; (i=1,\dots,3^{N-k})\). The sought answer is \(f(N,1)\).

Initially, for \(k=0\) we clearly have \(f(0,i)=1 \; (i=1,\dots,3^N)\).

Now we consider \(k \geq 1\). \(A_i^{(k)}\) is the majority of \(A^{(k-1)}_{3i-2},A^{(k-1)}_{3i-1},A^{(k-1)}_{3i}\). Among \(A^{(k-1)}_{3i-2},A^{(k-1)}_{3i-1},A^{(k-1)}_{3i}\), there are exactly two or three values equal to \(A_i^{(k)}\). We do casework depending on this value.

  • If three values are the same: To flip the value \(A^{(k)}_i\), we need to flip at least two of \(A^{(k-1)}_{3i-2}\), \(A^{(k-1)}_{3i-1}\), and \(A^{(k-1)}_{3i}\). Thus, \(f(k,i)\) is the sum of smallest two of \(f(k-1,3i-2),f(k-1,3i-1),f(k-1,3i)\).
  • If two values are the same: without loss of generality, we may assume that \(A^{(k)}_i=A^{(k-1)}_{3i-2}=A^{(k-1)}_{3i-1}\). Then, we do not need to flip \(A^{(k-1)}_{3i}\), while flipping one of \(A^{(k-1)}_{3i-2}\) and \(A^{(k-1)}_{3i-1}\). We can flip the smaller of \(f(k-1,3i-2)\) and \(f(k-1,3i-1)\), so \(f(k,i)=\min\{f(k-1,3i-2),f(k-1,3i-1)\}\).

Therefore, by filling the table in ascending order of \(k\), we can find all \(f(k,i)\). The time complexity is \(O(3^N)\).

It is convenient to use recursive functions.

Sample code (Python)

投稿日時:
最終更新: