Quadrilateral Inequality Optimization

Quadrilateral Inequality Optimization

fill the pit

Introduction

In the dp problem,
we often encounter such a kind of problem, its dp equation is such that
\[f[i][j] = min \{ f[i][k] + f[k + 1][j]+ cost[i][j] \} \]
So our complexity is generally \(O(n^3)\)
Let \( a < b <= c < d\)
If there is \(f[a] [c]+f[b][d]<=f[b][c]+f[a][d]\)
then this thing is said to satisfy the quadrilateral inequality
Theorem
Let the w function be the value function, for the above question \(w(i,j) = cost[i][j]\)
If \(w(i,j)\) satisfies \(w(a,d)>=w(b,c)\) then it is called w is monotonic with respect to the interval inclusion relation
Define s(i,j) to represent the optimal decision point of dp(i,j)
There are the following theorems

1. If w satisfies both monotonicity and quadrilateral inequality, then f is also quadrilateral inequality
2. If f satisfies the quadrilateral inequality, then s is monotonic, that is, \(s(i,j)\leq s(i,j+1)\leq s (i+1,j+1)\)
3.w satisfies the quadrilateral inequality if and only if \(w(i,j)+w(i+1,j+1)<=w(i,j+1) + w(i+1,j)\)

How to use it?
Originally in dp, the range of enumeration k is \((i,j)\) , and the transition complexity is \(O(n)\)
If f satisfies the quadrilateral inequality, then \(s[i][j- 1] \leq s[i][j] \leq s[i+1][j]\)
Then we reverse i and push j, make up for it, then transfer the complexityshare equallyIt is \(O(1)\) ,
so you only need to prove that w is convex and monotonic, record s when dp, you can reduce the one-dimensional complexity

Prove that w satisfies the quadrilateral inequality

will not (escape

Note that the dp transition equation that can be optimized with quadrilateral inequality is not the only one above
. For example: for
\[f[i][j] = min \{ f[i-1][k] + w(k+1,j ) \} \]
For this equation,
if we prove that w is convex, then f is also convex, we can also use quadrilateral inequality to optimize. The
key is how to prove it .
With mlystcall,
we can know how to bypass the proof

If we think an equation can be optimized by quadrilateral inequality, print out all its decision points, that is, the s matrix, and observe whether it is monotonic in each row and column. If it is monotonic, it means that this equation can be optimized by quadrilateral inequality.

Second, pay attention to a few points

1. It should be noted that the decision point is monotonic in those ranges, which should be easily reflected in practical problems. For example, the decision point in the interval dp \(s[i][j] | j>i\) The situation is 2. Pay attention to the
enumeration order

Then the rest is
to check the monotonicity of the table.
It is said that the optimization related to the quadrilateral inequality, there is another one called convex complete monotonicity, which is also used to optimize the decision monotonicity after the proof. no no no...

The code w attached to the merging of stones
satisfies the quadrilateral inequality, because at this time the intersecting interval sum, etc. and the inclusive interval sum
cannot satisfy the monotonicity of finding the maximum w? Then the decision is only taken at the two breaks of the interval?

#include<cstdio> 
#include<algorithm> 
inline int read() {
    int x = 0,f = 1;
    char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
    return x * f;
} 
const int maxn = 2007; 
int dp[maxn][maxn],s[maxn][maxn],cnt[maxn]; 
int sum[maxn]; 
int dpm[maxn][maxn];
int main () { 
    int n = read();
    for(int i = 1;i <= n;++ i) cnt[n + i] = cnt[i] = read(),sum[i] = sum[i - 1] + cnt[i]; 
    for(int i = n + 1;i <= n * 2;++ i) sum[i] = sum[i - 1] + cnt[i]; 
    for(int i = 1;i <= 2 * n;++ i) s[i][i] = i; 
    for(int i = n * 2 - 1;i >= 1;-- i) { 
        for(int j = i + 1;j <= n * 2;++ j) { 
            int tmp = 0x7fffffff,loc = 0; 
            dpm[i][j] = std::max(dpm[i][j - 1],dpm[i + 1][j]) + sum[j] - sum[i - 1]; 
            for(int k = s[i][j - 1];k <= s[i + 1][j];++ k) { 
                if(tmp >dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1]) { 
                    tmp = dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1]; 
                    loc = k; 
                } 
            } 
            dp[i][j] = tmp; 
            s[i][j] = loc; 
        } 
    } 
    int ansm = 0x7fffffff,ansx = 0;
    for(int i = 1;i <= n;++ i) {
        ansm = std::min(dp[i][i + n - 1],ansm) ; 
        ansx = std::max(dpm[i][i + n - 1],ansx) ; 
    }
    printf("%d\n%d",ansm,ansx);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325362686&siteId=291194637