Optimization of the Codevs3002 of the stone merging with quadrilateral inequalities

Source: Internet
Author: User

  • Title Description Description
    There are n heap of stones in a row, each pile of stones have a weight of w[i], each merge can merge adjacent two piles of stones, the cost of a merger is the weight of two piles of stone and w[i]+w[i+1]. Ask what sort of merger sequence it takes to minimize the total merger cost.

  • Enter a description input Description
    The first line is an integer n ( n≤ )
    Second row n integers w 1 , w 2 ... w n ( w i ≤ the)

  • outputs description output Description
    An integer representing the minimum consolidation cost

  • sample input to sample
    4
    4 1 1 4

  • sample output Sample outputs
    18

  • Solving
    Write out the normal interval DP equation and find that the time complexity is o ( n 3 ) , can't pass the subject. This problem can be optimized with quadrilateral inequalities.
    There is an article called "Quadrilateral inequalities of dynamic programming acceleration principle" which introduces the quadrilateral inequalities very well.
    The state transfer equation, which is like the subject, can be optimized with quadrilateral inequalities, which is no longer proved. (It is not possible to take the optimized and bare dp of the pair shoot Ah, quadrilateral inequalities are not difficult to write)
    Here is a description of the boundary:
    When there is only a heap of stones, F(i,i)=0 , f ( i , I ) The maximum value in i is taken from the office, so f ( i , I ) The maximum value of the corresponding decision variable is s(i,i)=i 。
    The boundary of the maximum value of the decision variable can be obtained naturally, as long as the value of the boundary is analyzed and the argument is equal to what is taken. Transfer later only in the update F Update at the same time s Can.

  • Code

#include <cstdio>#include <algorithm>using namespace Std;const int maxn = 3010, oo = 1000000000;int f[MAXN][MAXN], s[MAXN][MAXN];int N, a[MAXN], w[MAXN][MAXN];void Init () {scanf ("%d", &n);for (int i = 1; I <= n; ++i)    {scanf ("%d", &a[i]);W[i][i] = a[i];    }for (int i = 1, i <= N; ++i) for (int j = i + 1; j <= N; ++j)W[i][j] = w[i][j-1] + a[j];}void work () {for (int i = 1; I <= n; ++i) s[i][i] = i;for (int p = 1, p < n; ++p) for (int i = 1; I <= n-p; ++i)    {Int J = i + P;f[i][j] = oo;for (int k = s[i][j-1]; k <= s[i + 1][j]; ++k)if (F[i][j] > F[i][k] + f[k + 1][j] + w[i][j])            {F[i][j] = F[i][k] + f[k + 1][j] + w[i][j];s[i][j] = k;//Because K is incremented here, so you don't have to take Max            }    }printf ("%d", F[1][n]);}int Main () {init ();Work ();return 0;}

Optimization of the Codevs3002 of the stone merging with quadrilateral inequalities

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.