Slope Optimization

Example Introduction

「HNOI2008」Toy Packing

There are nn toys arranged in a row, and the value of toy ii is cic_i. We need to divide these nn toys into several segments. For a segment [l,r][l,r], its cost is (r−l+∑i=lrci−L)2(r-l+\sum_{i=l}^r c_i-L)^2, where LL is a constant. Find the minimum cost of the partition.

1≤n≤5×104,1≤L,ci≤1071\le n\le 5\times 10^4, 1\le L, c_i\le 10^7.

The Naive DP Approach

Let fif_i denote the minimum cost of partitioning the first ii items into several segments.

State transition equation: $f_i=\min_{j [!note]+ Toy Packing (Modified)

There are nn toys arranged in a row, and the value of toy ii is cic_i. We need to divide these nn toys into several segments. For a segment [l,r][l,r], its cost is (r−l+∑i=lrci−L)2(r-l+\sum_{i=l}^r c_i-L)^2, where LL is a constant. Find the minimum cost of the partition.

1≤n≤5×104,1≤L≤107,−107≤ci≤1071\le n\le 5\times 10^4,1\le L\le 10^7,-10^7\le c_i\le 10^7.

The only difference between this problem and "Toy Packing" is that the toy values may be negative. Continuing the previous approach, let fif_i denote the minimum cost of partitioning the first ii items into several segments.

The state transition equation is fi=min⁡j<i{fj+(prei−prej+i−j−1−L)2}f_i=\min_{j<i}\{f_j+(pre_i-pre_j+i-j-1-L)^2\}.

Here, prei=∑j=1icjpre_i = \sum_{j=1}^i c_j.

Apply the same transformation to the equation:

fi−(si−L′)2=min⁡j<i{fj+sj2+2sj(L′−si)}f_i - (s_i-L')^2=\min_{j<i}\{f_j+s_j^2 + 2s_j(L'-s_i) \}

However, two conditions no longer hold:

  1. The slopes of the lines are no longer monotonic;
  2. 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 CDQ(l,r)\text{CDQ}(l,r) represent computing fi,i∈[l,r]f_i,i\in [l,r]. Consider CDQ(1,n)\text{CDQ}(1,n):

  • We first call CDQ(1,mid)\text{CDQ}(1,mid) to compute fi,i∈[1,mid]f_i,i\in[1,mid]. Then we build a convex hull from the decision points in this interval [1,mid][1,mid] and use this convex hull to update fi,i∈[mid+1,n]f_i,i\in [mid+1,n]. 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 fif_i for i∈[mid+1,n]i \in [mid+1,n] by the slopes kik_i 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 [mid+1,n][mid+1,n], if its optimal decision lies in this interval [1,mid][1,mid], it is updated to the optimal answer in this step. After this step, we find that all points in [1,mid][1,mid] 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 CDQ(mid+1,n)\text{CDQ}(mid+1,n) to solve the remaining problem in the right interval.

The time complexity is O(nlog⁡2n)O(n\log^2 n).

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.

Exercises

This content comes from OI Wiki: Slope Optimization. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.