3002 Stone Merge 3
time limit: 1 sspace limit: 256000 KBtitle level: Diamonds Diamond SolvingTitle 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
First line an integer n (n<=3000)
Second row n integers w1,w2...wn (WI <= 3000)
Output description
Output Description
An integer representing the minimum consolidation cost
Sample input
Sample Input
4
4 1 1 4
Sample output
Sample Output
18
Data range and Tips
Data Size & Hint
The data range is larger than the "gravel merge"
Category labels
Tags Click here to expandDynamic programming of interval DP monotonic DPThe
following:
theorem One: If w satisfies both quadrilateral inequalities and decision Monotonicity, then D also satisfies the quadrilateral inequalities
Theorem two: When the condition of theorem one satisfies, let d[i,j] take the minimum value of K is K[i,j], then k[i,j-1]<=k[i,j]<=k[i+1,j]
Theorem Three: W is convex and only if w[i,j]+w[i+1,j+1]<=w[i+1,j]+w[i,j+1]
AC Code:
#include <cstdio> #include <cstring> #define MIN (A, B) a>b?b:ausing namespace std; #define N 3010int N,s[n], F[n][n],b[n][n];int Main () {scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%d", &s[i]), s[i]+=s[i-1];for (int i=1;i<=n;i++) b[i][i]=i;for (int j=2;j<=n;j++) {for (int i=j-1;i>=1&&j-i+1<=n;i--) {f[i][j]= 0x7fffffff;for (int k=b[i][j-1];k<=b[i+1][j];k++) {if (f[i][j]>f[i][k]+f[k+1][j]+s[j]-s[i-1]) {f[i][j]=f[i][k ]+f[k+1][j]+s[j]-s[i-1];b[i][j]=k;}}}} printf ("%d\n", F[1][n]); return 0;}
3002 Stone Merge 3