The topic is very simple, and ordinary stone merge process no difference, but the cost has become a polynomial, if a continuous arbitrary stone weights and X, then the cost to f (x) = Sigma (a[i] * x^i), the n heap of stones to merge into a team of the minimum cost.
For violent practices, the complexity is O (n^3), so to optimize
We know that when a, B, C, D (a <= B < c <= D) when there is cost[a][c] + cost[b][d] <= cost[a][d] + cost[b][c], we call it satisfies the quadrilateral inequalities, set P[I][J] to indicate When the interval [I, j] is chosen for the optimal decision of the subscript, it can be proved that there is p[i][j-1] <= p[i][j] <= p[i + 1][j] (it took me a long time to finally prove), nothing can prove the next look, you can also remember this conclusion.
At this time when the interval DP, the calculation interval [I, j] the optimal solution, as long as the enumeration [P[i][j-1], P[i + 1][j]] can, because the array p value is [1, n] and is monotonous, so the total complexity of the enumeration is O (n), and finally the complexity of the interval enumeration, the total complexity is O (n^2
So for the general topic, need to prove that only DP volume is not satisfied with the quadrilateral inequalities, for this problem is to prove:
Set SUM (A, B) = x, sum (b, c) = Z, sum (c, d) = y;
There is f (x + Z) + f (y + Z) <= f (z) + f (x + y + z), which proves:
Sigma (A[i] * ((x + z) ^i + (y + z) ^i-z^i-(x+y+z) ^i)) <= 0, converted to proof:
(x + z) ^ n + (y + z) ^ n-z ^ n-(x + y + z) ^ n <= 0 Constant set.
It is obvious that this inequality can be easily proved by using mathematical inductive method.
1#include <map>2#include <Set>3#include <stack>4#include <queue>5#include <cmath>6#include <ctime>7#include <vector>8#include <cstdio>9#include <cctype>Ten#include <cstring> One#include <cstdlib> A#include <iostream> -#include <algorithm> - using namespacestd; the #defineINF 0x3f3f3f3f - #defineInf (-((LL) 1<<40)) - #defineLson k<<1, L, (L + R) >>1 - #defineRson k<<1|1, ((L + R) >>1) + 1, R + #defineMem0 (a) memset (A,0,sizeof (a)) - #defineMem1 (a) memset (A,-1,sizeof (a)) + #defineMem (A, B) memset (A, B, sizeof (a)) A #defineFIN freopen ("In.txt", "R", stdin) at #defineFOUT freopen ("OUT.txt", "w", stdout) - #defineRep (I, A, b) for (int i = A; I <= B; i + +) - -template<classT> T Cmp_min (t A, T b) {returnA <b;} -template<classT> T Cmp_max (t A, T b) {returnA >b;} -template<classT> T MAX (t A, T b) {returna > B?a:b;} intemplate<classT> T MIN (t A, T b) {returnA < b?a:b;} -template<classT> T GCD (t A, T b) {returnB? GCD (b, a%b): A; } totemplate<classT> T LCM (t A, T b) {returnA/GCD (A, b) *b; } + - //typedef __int64 LL; thetypedefLong LongLL; * Const intMAXN =51000; $ Const intMAXM =110000;Panax Notoginseng Const DoubleEPS = 1e-4; - //LL MOD = 987654321; the + intT, N, M, s[1100], a[Ten]; ALL p[1100][1100], dp[1100][1100]; the +LL Fun (intx) { -LL ans =0, p =1; $Rep (I,0, M) { $Ans + = a[i] *p; -P *=x; - } the returnans; - }Wuyi the intMain () - { Wu //FIN; - while(~SCANF ("%d", &t)) while(t--) { Aboutscanf"%d", &n); $Rep (I,1, N) scanf ("%d", S + i), s[i] + = s[i-1]; -scanf"%d", &m); -Rep (I,0, m) scanf ("%d", A +i); - MEM0 (DP); mem0 (p); ARep (Len,1, N) { +Rep (I,1, N-len +1) { the intj = i + len-1; -LL cost = Fun (S[j]-s[i-1]); $ if(Len <=1) {Dp[i][j] =0; P[I][J] =i;} the ElseRep (k, P[i][j-1], P[i +1][j]) { the if(Dp[i][k] + dp[k+1][J] + cost < Dp[i][j] | | DP[I][J] = =0) { theP[I][J] =K; theDP[I][J] = Dp[i][k] + dp[k+1][J] +Cost ; - } in } the } the } Aboutcout << dp[1][n] <<Endl; the } the return 0; the}
Stone Merging (quadrilateral inequality optimization)