K - Storing Roll Cake 解説
by
Lulusphere
印刷ページ
Suppose the roll cake is eventually divided into several pieces. Since only the right end of each piece is visible, the total beauty is the sum of \(V_i\) over the right endpoints \(i\) of the final pieces.
Now consider the effort required to cut the cake. If the lengths of the final pieces are \(a_1,a_2,\ldots,a_m\), then the total effort is
\[ \sum_{1 \le p < q \le m} a_p a_q. \]
Thus, the order of cuts does not matter; only the final partition matters.
Let \(dp[i]\) be the maximum value obtainable after storing the first \(i\) pieces. Suppose the last piece consists of positions \(j+1,j+2,\ldots,i\). Then its length is \(i-j\), and its right endpoint is \(i\), so \(V_i\) is added to the beauty.
The newly added effort between the previous part of length \(j\) and the last piece of length \(i-j\) is
\[ j(i-j). \]
Therefore, we obtain the recurrence
\[ dp[i] = V_i+ \max_{\max(0,i-K)\le j<i} \left(dp[j]-j(i-j)\right). \]
Rearranging the expression gives
\[ dp[i] = V_i+ \max_{\max(0,i-K)\le j<i} \left(dp[j]+j^2-ij\right). \]
If we regard \(i\) as a variable, each \(j\) corresponds to a line
\[ y=-jx+(dp[j]+j^2). \]
Thus, we need to insert lines and query the maximum value at \(x=i\) among the currently valid lines. Since the slopes \(-j\) are monotone and the query points \(i\) are also monotone, this can be handled by a deque-based Convex Hull Trick.
The only issue is the condition
\[ i-K \le j < i. \]
To avoid deletions from the convex hull, divide the indices into blocks of length \(K\). Consider a block \([S,E]\). For \(i \in [S,E]\), valid candidates are divided into two types:
\[ i-K \le j < S \]
and
\[ S \le j < i. \]
The first type consists of candidates from the previous block. We process \(i\) from right to left. Then the set of valid candidates only grows, and the slopes of newly inserted lines are monotone. Hence, these candidates can be processed with a monotone CHT.
The second type consists of candidates inside the current block. We process \(i\) from left to right. After computing \(dp[i]\), we insert the line corresponding to \(j=i\). Again, both the insertion order and the query order are monotone, so a monotone CHT can be used.
For each \(i\), we take the maximum value among the two cases and add \(V_i\) to compute \(dp[i]\).
Therefore, the problem can be solved using only monotone Convex Hull Tricks, without any deletion operation. Since every line is inserted and removed from a deque only a constant number of times, the total time complexity is \(O(L)\).
投稿日時:
最終更新:
