C - Cost to Flip 解説 by evima
Call an element with \(A_i = p\) and \(B_i = q\) a “\((p, q)\)-type.” Then, the \(N\) elements can be classified into the following types: \((0, 0)\), \((0, 1)\), \((1, 0)\), and \((1, 1)\).
If any single element changes \(0 \rightarrow 1\) and then later \(1 \rightarrow 0\), you could omit these two operations altogether and reduce the total cost. Therefore, in an optimal sequence of operations, the value changes of each element, depending on its type, are limited to the following:
- \((0, 0)\) type: It remains \(0\) and does not change.
- \((0, 1)\) type: It changes exactly once from \(0 \rightarrow 1\).
- \((1, 0)\) type: It changes exactly once from \(1 \rightarrow 0\).
- \((1, 1)\) type: Either (A) or (B) below:
- (A) It remains \(1\) without changing.
- (B) It changes exactly once from \(1 \rightarrow 0\) and once from \(0 \rightarrow 1\).
Once it is fixed for each \((1, 1)\)-type element whether it is (A) or (B), the optimal sequence of operations in that case is uniquely determined by the procedure \((\star)\) below.
\((\star)\) Perform the \(1 \rightarrow 0\) operations in descending order of \(C\) among those elements, then perform the \(0 \rightarrow 1\) operations in ascending order of \(C\).
In fact, if the operation order contradicts the above for some pair of operations, you can swap the order of those two operations and thereby reduce the total cost.
Moreover, let \(m\) be the number of \((1, 1)\)-type elements. When deciding whether each such element is (A) or (B), it suffices to consider only choices that satisfy:
There exists an integer \(0 \leq h \leq m\) such that the \(h\) elements with the smallest \(C\) among the \((1,1)\)-type choose (A), and the remaining elements choose (B).
Indeed, if there are two \((1, 1)\)-type elements \(A_i\) and \(A_j\) such that \(A_i\) chooses (B) while \(A_j\) chooses (A) even though \(C_i < C_j\), you can swap their roles to achieve a smaller total cost.
Hence, to solve the problem, we can try all \(h = 0, 1, 2, \ldots, m\), apply \((\star)\) for that fixed choice, and take the minimum total cost among them.
Concretely, when the choice of (A) or (B) is fixed for each \((1, 1)\)-type element (in particular when \(h\) is fixed), the total cost under \((\star)\) is given by
\[ \sum_{l = 1}^X (l-1)C_{i_l} + \sum_{l = 1}^Y lC_{j_l} + (X+Y)\sum_{l=1}^Z C_{k_l}. \]
Here, we suppose that there are \(X\) elements subjected to \(1 \rightarrow 0\) operations, in descending order of \(C\) as \(C_{i_1} \geq C_{i_2} \geq \cdots \geq C_{i_X}\), there are \(Y\) elements subjected to \(0 \rightarrow 1\) operations, in descending order of \(C\) as \(C_{j_1} \geq C_{j_2} \geq \cdots \geq C_{j_Y}\), and there are \(Z\) elements (of type \((1,1)\)) that choose (A), with their \(C\) values \(C_{k_1}, C_{k_2}, \ldots, C_{k_Z}\).
Computing this expression independently for all \(h = 0, 1, 2, \ldots, m\) would be too time-consuming. But by starting from \(h = 0\) and then increasing \(h\) by 1 each time, updating only the difference in cost (with appropriate preprocessing like prefix sums), we can meet the time limits.
投稿日時:
最終更新: