公式

B - Minimize Topological Order 解説 by evima 印刷ページ


For \(T\) to satisfy \(f(T) = P\), first, the root must be vertex \(P_1\). We assume this from now on.

Also, since \(T\) is a rooted tree, for \(P\) to be obtainable as a topological sort of \(T\), the parent of vertex \(P_i\) for \(i \ge 2\) must be one of vertices \(P_1,P_2,\dots,P_{i-1}\).

Furthermore, for \(i\) and \(j\) satisfying \(P_i > P_j\,(i < j)\), the parent of vertex \(P_j\) must be one of vertices \(P_i,P_{i+1},\dots,P_{j-1}\). If this were not satisfied, then when we build the lexicographically smallest topological sort up through vertex \(P_{i-1}\), the parent of vertex \(P_j\) would already have been chosen, so the next vertex chosen will not be vertex \(P_i\).

These conditions are also sufficient. For a rooted tree \(T\) with root \(P_1\) to satisfy \(f(T) = P\), it suffices that for every integer \(i\) satisfying \(1 \le i < N\), among the vertices whose parent is one of \(P_1,P_2,\dots,P_i\), the one with the smallest number is vertex \(P_{i+1}\); this can be derived from the conditions above.

Thus, for \(2 \le i \le N\), let \(L_i\) be the largest \(j\) satisfying \(P_j > P_i\,(j < i)\) (or \(1\) if no such \(j\) exists). Then this problem reduces to the following problem.

There is a sequence of integers $c=(0,0,\dots,0)$ of length $N$. For $i = 2,3,\dots,N$ in order, perform the following operation.

  • Choose an integer $j$ satisfying $L_i \le j \le i-1$, and increase $c_j$ by $1$.

Find the minimum possible value of $\sum_{i=1}^{N} A_i c_i^2$ at the end of the operations.

An important property is that if we take any two of the intervals \([L_2,1],[L_3,2],\dots,[L_N,N-1]\), they are either disjoint or one contains the other. That is, there do not exist intervals \([L_x,x-1]\) and \([L_y,y-1]\) satisfying \(L_x < L_y \le x-1 < y-1\). If such \(x\) and \(y\) existed, all of the following would hold.

  • $P_x < P_y$, since $L_y < x < y$
  • $P_{L_y} < P_x$, $L_x < L_y < x$
  • $P_{L_y} > P_y$, by the condition on $L_y$

This is a contradiction, so we have proved that no two intervals cross. Also, by the convexity of \(x^2\), the above problem can be rephrased as follows.

There are points $1,2,\dots,N$, and point $i$ has $A_i,3A_i,5A_i,\dots$ written on it. For $i = 2,3,\dots,N$ in order, perform the following operation.

  • Choose one integer written on one of the points $L_i,L_i+1,\dots,i-1$, and erase it.

Find the minimum possible value of the sum of the $N-1$ chosen integers.

This greedy method is valid: process the intervals in order from narrowest to widest, choosing the smallest integer currently available. Ties may be broken arbitrarily.

Let us prove this. Suppose there is an operation on interval \([l,r]\) that does not choose \(m\), one of the minimum integers in \([l,r]\), but instead chooses \(m'\). Take the first such operation.

  • If $m$ is never chosen in any of the $N-1$ operations:
  • We lose nothing by instead choosing $m$ rather than $m'$ in this operation.

  • If $m$ is chosen at some later interval $[l',r']$:
  • Since $[l,r]$ and $[l',r']$ share $m'$ in common, and $r-l \le r'-l'$, by the interval property shown earlier, $[l',r']$ contains $[l,r]$. Hence we lose nothing by instead choosing $m$ in $[l,r]$ and $m'$ in $[l',r']$.

Thus, we have proved the existence of an optimal solution that chooses \(m\) in \([l,r]\). By repeating this argument, we can also prove that the solution obtained by the greedy method is optimal.

The part that computes \(L\) can be done in \(\mathrm{O}(N)\) using a stack, and the greedy method can be done in \(\mathrm{O}(N \log N)\) using a segment tree, so this solution is \(\mathrm{O}(N \log N)\) overall.

投稿日時:
最終更新: