Ex - Multiply or Divide by 2 Editorial by Kiri8128


Write \(n>0\) in a format \(n=2^x\times y\), where \(y\) is odd. When \(n=0\), let \((x, y) = (0, 0)\). We can match integers greedily in a descending order of y because an “operation” does NOT increase the value of \(y\).

Within a group with same \(y\), match integers with largest \(x\)’s from \(A\) and \(B\). After continuing this, if \(B\) is left, it’s failed. If \(A\) is left, move the left integers to smaller \(y\) group, i.e., you can apply \(x+1\) operations to \(2^x\times y\).

Managing \(y\)’s by a priority queue, this can be achieved in \(O(N\log N \log A_{max})\) time.

posted:
last update: