Quadrilateral inequality optimization _ gravel merging problem _ C ++ and inequality _ c

Source: Internet
Author: User

Quadrilateral inequality optimization _ gravel merging problem _ C ++ and inequality _ c

In dynamic planning, the following state transition equation is often encountered:

M (I, j) = min {m (I, k-1), m (k, j)} + w (I, j) (I ≤ k ≤ j) (min can also be changed to max)

M (I, j) indicates an optimal value on the interval [I, j. W (I, j) indicates the extra cost during the transfer. The time complexity of this equation is O (N3)

In the following, we use a quadrilateral inequality to optimize the above equations. First, we will introduce what is "monotonicity in the interval" and "quadrilateral inequality"

1. monotonicity of the interval: for I ≤ I '<j ≤ J', w (I', j) ≤ w (I, j '), it indicates that w has the monotonicity of Range inclusion. (It can be understood that if the cells are included in a large interval, the w value between the cells cannot exceed the w value of the large interval)

2. quadrilateral inequality: for I ≤ I '<j ≤ J', w (I, j) + w (I', J') ≤ w (I ', j) + w (I, j '). We call the function w to satisfy the Quadrilateral inequality. (It can be interpreted as the sum of w in two staggered intervals and w in the same zone and large interval)

 

Two theorems are given below:

1. If the preceding w function satisfies the monotonicity and quadrilateral inequality of the interval, then the m function also satisfies the Quadrilateral inequality.

Then we define s (I, j) to indicate the subscript corresponding to m (I, j) when the optimal value is obtained (that is, when I ≤ k ≤ j, the w value at k is the largest, then s (I, j) = k ). The following theorem exists:

2. If m (I, j) satisfies the Quadrilateral inequality, s (I, j) is monotonic, that is, s (I, j) ≤ s (I, j + 1) ≤ s (I + 1, j + 1 ).

 

Well, with the two theorem above, we find that if the w function satisfies the properties of the range containing monotonic and quadrilateral inequality, then s (I, J-1) ≤ s (I, j) ≤ s (I + 1, j ).

That is, the original state transition equation can be rewritten to the following formula:

M (I, j) = min {m (I, k-1), m (k, j)} + w (I, j) (s (I, J-1) ≤ k ≤ s (I + 1, j) (min can also be changed to max)

Since the state transition equation enumerates the interval length L = j-I, and the length of s (I, J-1) and s (I + 1, j) is L-1, it is previously computed and can be called directly.

In addition, the interval length can be at most n. For fixed length L, there are also n different states, so the time complexity is O (N ^ 2 ), the original time complexity is O (N ^ 3), which is optimized!

In the future, we only need to consider using quadrilateral inequalities based on the form of the equation and whether the w function satisfies two properties.

 

M (I, j) is used to describe the status above, and dp [I] [j] is used later, which means the same meaning.

Take the issue of stone merge as an example.

For example, there are 6 piles of stones, each of which is 3 4 6 5 4 2 in sequence.

Because it is a combination of adjacent stones, you cannot use greed (take the smallest two heaps of merged at a time). You can only use dynamic conversion. (Note: the merge of the last heap and the first heap must be considered .)

For example, a plan to merge stones:

First merge 3 4 6 5 4 2-> 7

Merge for the second time 7 6 5 4 2-> 13

Merge for the third time 13 5 4 2-> 6

Fourth merge 13 5 6-> 11

Fifth merge 13 11-> 24

Total score = 7 + 6 + 11 + 13 + 24 = 61 obviously, it is better than the merge plan obtained by greedy method (score: 62.

  

Dynamic regression analysis is similar to matrix concatenation and so on, and a recursive equation is obtained:

If dp [I] [j] is set, it indicates the optimal value from I to j, and sum [I] [j] indicates the total number of stones from I to j.

(You can calculate all sum [I] at the beginning of the calculation, indicating to find the total number of all 1st heap to I heap. Sum [I] [j] = sum [j]-sum [I]. .)

There is a state transfer formula:

      

Here I <= k <j

The general solution requires O (n ^ 3 ). The quadrilateral inequality is used for optimization.

First, determine whether the interval monotonicity and quadrilateral inequality are met.

I 'J J'

3 4 6 5 4 2

Monotonic:

W [I ', j] = 4 + 6 + 5 = 15 w [I, j'] = 3 + 4 + 6 + 5 + 4 + 2 = 24

Therefore, w [I ', j] <= w [I, j'] satisfies monotonicity.

Quadrilateral inequality:

W [I, j] + w [I ', j'] = (3 + 4 + 6 + 5) + (4 + 6 + 5 + 4 + 2) = 18 + 21 = 39

W [I ', j] + w [I, J'] = (4 + 6 + 5) + (3 + 4 + 6 + 5 + 4 + 2) = 15 + 24 = 39

Therefore, w [I, j] + w [I ', J'] <= w [I', j] + w [I, J']

Therefore, the Quadrilateral inequality can be used to optimize the combination of stones.

 

By using quadrilateral inequality, the number of state transitions of the original recursive equation is compressed (that is, the value range of k is reduced ).

S [I] [j] = min {k | dp [I] [j] = dp [I] [k-1] + dp [k] [j] + w [I] [j]}, that is, the optimal k value for dp [I] [j] is calculated (in this example, the optimization is the minimum value)

It can also be called the k value in the optimal decision. The value range of k in the state transition equation can be changed:

S [I, J-1] <= s [I, j] <= s [I + 1, j]

Boundary: s [I, I] = I

Because the value of s [I, j] is saved and updated when m [I, j] gets the optimal value, s [I, J-1] And s [I + 1, j] has been calculated in the calculation of dp [I] [J-1] and dp [I + 1] [j.

Therefore, the value range of s [I] [j], that is, k, is easily determined.

According to the improved state equation and the defined equation s [I] [j], the values of all States can be quickly calculated. The calculation process can be shown in the following table (similar to a matrix concatenation table ).

Status table (if it is a combination of ring stones, you need to create a table with 2n * 2n)

3 4 6 5 4 2

  

For example:

Calculate dp [1] [3], because s [1] [2] = 1, s [2] [3] = 2, then the value range of k value is 1 <= k <= 2

Then, dp [1] [3] = min {dp (1, 1) + dp (2, 3) + 13, dp (1, 2) + dp (3, 3) + 13 }= min {10 + 13, 7 + 13} = 20, fill it in the status table. At the same time, because k of the optimal value is 2, it is filled into the s table.

Similarly, values in other State tables and s tables can be calculated.

Dp [2] [4] = min {dp (2, 2) + dp (3, 4) + 15, dp (2, 3) + dp (4, 4) + 15 }= min {11 + 15, 10 + 15} = 25

K = 3

We can see from the table that when calculating dp [2] [5], because s [I, J-1] = s [2, 4] = 3, s [I + 1, j] = s [3, 5] = 3. At this time, the value range of k has been limited to only one, greatly shortening the time to find the optimal solution.

 

The program code is provided here:

 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4  5 const int N=205; 6 const int INF=0x7fffffff; 7 int n; 8 int a[N],sum[N],dp[N][N],s[N][N]; 9 void f(); 10 int main()11 {12      while(~scanf("%d",&n))13      {14          sum[0]=0;15         for (int i=1;i<=n;i++)16         {17             scanf("%d",&a[i]);18             sum[i]=sum[i-1]+a[i];19         }20         f();21         printf("%d\n",dp[1][n]);22      }23      return 0;24 25 }26 void f()27 {28      for (int i=1;i<=n;i++) dp[i][i]=0,s[i][i]=i;29      for (int r=1;r<n;r++)30      {31          for (int i=1;i<n;i++)32         {33             int j=i+r;34             if(j>n) break;35             dp[i][j]=INF;36             for (int k=s[i][j-1];k<=s[i+1][j];k++)37             {38                 if(dp[i][j]>dp[i][k]+dp[k+1][j])39                 {40                     dp[i][j]=dp[i][k]+dp[k+1][j];41                     s[i][j]=k;42                 }43             }44             dp[i][j]+=sum[j]-sum[i-1];45         }46     }47 }

 

 

 

This article is reproduced from Netease blog:

Http://blog.163.com/dqx_wl/blog/static/2396821452015111133052112/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.