Do not understand, do a few more to look at first
This topic is very basic to the slope DP 's entry question.
Test instructions very clearly, is the output sequence a[n], the cost of each successive output is the continuous output of the number and the square plus constant m
Let us ask for the minimum value of this fee.
Set Dp[i] represents the minimum cost for the first I of the output, then the following DP equation:
dp[i]= min{dp[j]+ (sum[i]-sum[j]) ^2 +m} 0<j<i
Where Sum[i] represents the first I item and of the number.
I believe we can understand the above equation.
The complexity of solving the equation directly is O (n^2).
The size of 500000 is clearly timed out. The following explains how to use slope optimization DP to reduce the complexity of one dimension.
We first assume that in the calculation of Dp[i], K<j, J Point than the K-point excellent.
That is
dp[j]+ (Sum[i]-sum[j]) ^2+m <= dp[k]+ (sum[i]-sum[k]) ^2+m;
The so-called J-ratio K-merit is a small value in the DP equation
Sorting the above equations is easy to get:
[(Dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k])]/2 (sum[j]-sum[k]) <=sum[i].
Note that in the collation to consider the positive and negative, involving the direction of the non-equal.
Left we found if order: Yj=dp[j]+sum[j]*sum[j] xj=2*sum[j]
Then it becomes the slope expression: (Yj-yk)/(XJ-XK) <= sum[i];
And the right side of the inequality is incremented.
So we can see the following two points: we make g[k,j]= (Yj-yk)/(XJ-XK)
First: If the above inequalities are established, then it is said that J is better than K, and as I increases the above inequalities must be established, that is, after I calculate the DP value, J is better than K. Then K can be eliminated.
Second: If K<j<i and G[k,j]>g[j,i] then J can be eliminated.
Assuming G[j,i]<sum[i] is the I-ratio J, then J has no value in existence.
Conversely if g[j,i]>sum[i] so also have g[k,j]>sum[i] so K is better than J, then J can be eliminated.
So this is equivalent to maintaining a convex figure, the slope is gradually increasing.
Maintained through a queue.
So for this question we can summarize the slope optimization practices as follows:
1, use a monotone queue to maintain the solution set.
2, suppose that there is an element a B C in the queue from beginning to end. So when D is on the team, we maintain the convex nature of the queue, that is, if G[D,C]<G[C,B], then delete the C point. Until G[d,x]>=g[x,y] is found and the D point is added to the position.
3, the solution time, starting from the team head, if there is an element a B C, when I point to solve, if g[b,a]<sum[i], then the b point is better than a point, a point can be excluded, so a out of the team. Last DP[I]=GETDP (Q[head]).
1 /*2 HDU 35073 4 */5 6#include <stdio.h>7#include <iostream>8#include <string.h>9#include <queue>Ten using namespacestd; One Const intmaxn=500010; A - intDP[MAXN]; - intQ[MAXN];//Queue the intSUM[MAXN]; - - inthead,tail,n,m; - //dp[i]= min{dp[j]+m+ (Sum[i]-sum[j]) ^2}; + intGETDP (intIintj) - { + returndp[j]+m+ (Sum[i]-sum[j]) * (sum[i]-sum[j]); A } at - intGetUp (intJintK//Yj-yk Part - { - returndp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]); - } - intGetdown (intJintk) in { - return 2* (sum[j]-sum[k]); to } + - intMain () the { * //freopen ("In.txt", "R", stdin); $ //freopen ("OUT.txt", "w", stdout);Panax Notoginseng while(SCANF ("%d%d", &n,&m) = =2) - { the for(intI=1; i<=n;i++) +scanf"%d",&sum[i]); Asum[0]=dp[0]=0; the for(intI=1; i<=n;i++) +sum[i]+=sum[i-1]; -Head=tail=0; $q[tail++]=0; $ for(intI=1; i<=n;i++) - { - //turn the slope to multiply, note the order, or the direction of the non-equal sign will change. the while(head+1<tail && GetUp (q[head+1],q[head]) <=sum[i]*getdown (q[head+1],q[head])) -head++;Wuyidp[i]=GETDP (I,q[head]); the while(head+1<tail && GetUp (i,q[tail-1]) *getdown (q[tail-1],q[tail-2]) <=getup (q[tail-1],q[tail-2]) *getdown (i,q[tail-1])) -tail--; Wuq[tail++]=i; - } Aboutprintf"%d\n", Dp[n]); $ } - return 0; -}
HDU 3507 Slope dp