3002 stone merge 3, stone merge

Source: Internet
Author: User

3002 stone merge 3, stone merge
Time Limit: 1 s space limit: 256000 KB title level: DiamondQuestionView running resultsDescriptionDescription

There are n piles of stones in a column, each pile of stones has a weight w [I], each merge can combine adjacent two piles of stones, the cost of one merge is the weight of two piles of stones and w [I] + w [I + 1]. Ask how to arrange the merge sequence to minimize the total merge cost.

Input description Input Description

The first line is an integer n (n <= 3000)

N integers w1, w2. .. wn (wi <= 3000) in the second row)

Output description Output Description

An integer indicates the minimum merge cost.

Sample Input Sample Input

4

4 1 1 4

Sample output Sample Output

18

Data range and prompt Data Size & Hint

The data range is extended compared to the "Stone merge"

CATEGORY tag Tags click here to expandDynamic Programming interval-type DP monotonic DP enumeration Length + quadrilateral inequality Optimization
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define lli long long int  7 using namespace std; 8 const int MAXN=5001; 9 const int maxn=0x7fffffff;10 void read(int &n)11 {12     char c='+';int x=0;bool flag=0;13     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}14     while(c>='0'&&c<='9')15     x=(x<<1)+(x<<3)+c-48,c=getchar();16     flag==1?n=-x:n=x;17 }18 int n;19 int a[MAXN]; 20 int sum[MAXN];21 int dp[MAXN][MAXN];22 int mid[MAXN][MAXN];23 int  main()24 {25     read(n);26     for(int i=1;i<=n;i++)27         read(a[i]);28     for(int i=1;i<=n;i++)29         sum[i]=a[i]+sum[i-1];30     for(int i=1;i<=n;i++)31         dp[i][i]=0,mid[i][i]=i;32     for(int len=1;len<=n-1;len++)33     {34         for(int i=1;i<=n-len;i++)35         {36             int j=len+i;37             dp[i][j]=maxn;38             for(int k=mid[i][j-1];k<=mid[i+1][j];k++)39             {40                 if(dp[i][j]>dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1])41                 {42                     dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];43                     mid[i][j]=k;44                 }45             }46         }47     }48     49     printf("%d",dp[1][n]);50     return 0;51 }

 

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.