Example Introduction
「HNOI2008」Toy Packing
There are toys arranged in a row, and the value of toy is . We need to divide these toys into several segments. For a segment , its cost is , where is a constant. Find the minimum cost of the partition.
.
The Naive DP Approach
Let denote the minimum cost of partitioning the first items into several segments.
State transition equation: $f_i=\min_{j [!note]+ Toy Packing (Modified)
There are toys arranged in a row, and the value of toy is . We need to divide these toys into several segments. For a segment , its cost is , where is a constant. Find the minimum cost of the partition.
.
The only difference between this problem and "Toy Packing" is that the toy values may be negative. Continuing the previous approach, let denote the minimum cost of partitioning the first items into several segments.
The state transition equation is .
Here, .
Apply the same transformation to the equation:
However, two conditions no longer hold:
- The slopes of the lines are no longer monotonic;
- The x-coordinates of the decision points added each time are no longer monotonic.
We still consider maintaining the convex hull.
When finding the optimal decision point, that is, when cutting the convex hull with a line, we replace finding the head of the monotone queue with binary search on the convex hull. We binary-search for the convex hull edge whose slope is closest to the line's slope, and thereby find the optimal decision.
When adding a decision point, that is, adding a point to the convex hull, we have two ways to maintain it.
The first is to maintain the convex hull directly with a balanced tree. Then the binary search for the decision point becomes a binary search on the balanced tree, and inserting a decision point becomes inserting a node into the balanced tree and deleting the points expelled from the convex hull. This approach is conceptually simple but cumbersome to implement.
Below we introduce an approach based on CDQ divide and conquer.
Let represent computing . Consider :
-
We first call to compute . Then we build a convex hull from the decision points in this interval and use this convex hull to update . At this point, the set of decision points is fixed. Unlike before, when decision points were added while computing DP values, we can first sort the for by the slopes of their lines, and then use a monotone queue to compute the DP values. Of course, DP values can also be computed by binary search on the static convex hull.
-
For every point in , if its optimal decision lies in this interval , it is updated to the optimal answer in this step. After this step, we find that all points in have already played their full role, and whether they remain in the convex hull no longer affects subsequent answer updates. Therefore, we can discard the decision points in this interval and use to solve the remaining problem in the right interval.
The time complexity is .
Comparing "Toy Packing" and "Toy Packing (Modified)", we can summarize the following two points:
- Binary search/CDQ/balanced trees and other methods can optimize the computation of a DP equation and reduce complexity to some extent, but cannot change the equation itself.
- The properties of a DP equation depend on the characteristics of the data, while the DP equation itself depends on the mathematical model in the problem.
Summary
Slope-optimization DP requires flexible application. Its purpose is to transform an optimization problem into a problem of finding an extremal intercept related to a convex hull in a two-dimensional plane. When an equation has unfavorable properties, data structures may sometimes be used to solve it; in that case, please consider the specific problem.