F - Diagonal Separation 2 解説 by en_translator
Let us take a closer look at the conditions. Intuitively, the conditions mandate the white cells are placed in stair-shape, but formally we can express them as follows:
There exists an integer sequence \((A_1, A_2, \ldots, A_N)\), with \(0 \leq A_i \leq N\), such that:
- The first \(A_i\) cells in the \(i\)-th row are painted white, and the others in black.
- \(A_1 \geq A_2 \geq \ldots \geq A_N\).
The first condition is the same as the one in the problem statement, while the second condition originates from the column-wise condition but restricts the ordering of \(A_i\) instead.
This interpretation allows us to perform a Dynamic Programming (DP) deciding the colors of cells one row by one. Let \(dp_{i, j}\) be the minimum number of operations to (re)paint the first \(i\) rows, with \(A_i = j\).
We will represent the values \(dp_{i, *}\) using the values \(dp_{i - 1, *} \). Let \(D_{i, j}\) be the number of operations required to make the first \(j\) cells white and the other cells black in the \(i\)-th row. Then \(dp_{i, j} = D_{i, j} + \displaystyle\min_{j \leq k \leq N} dp_{i - 1, k}\).
Naively evaluating the expression above costs \(\Theta(N^3)\) time, but one can take the cumulative sums or find each \(D_{i, j + 1}\) incrementally based on \(D_{i, j}\) in \(O(1)\) time, so that \(D_{i, j}\). Then \(\displaystyle\min_{j \leq k \leq N} dp_{i - 1, k}\) can also be computed efficiently by taking cumulative min from the last. Therefore, the answer can be found in a total of \(O(N^2)\) time.
投稿日時:
最終更新: