The main idea: there are N numbers a[n], each output continuous string, its cost is "this line number of squares plus a constant m". Ask how the output makes the total cost minimal. (n<=500000)
Analysis: The dynamic programming equation is: dp[i]=dp[j]+m+ (sum[i]-sum[j]) ^2;
One on the right is: sum[i]*sum[j]. Consider using a monotone queue.
Make Dp[i]=g,dp[j]+m+sum[i]^2+sum[j]^2=y,sum[j]=y,2*sum[i]=k
The upper formula becomes g=y-kx, that is, y=kx+g. This is a linear equation.
We want to make the G minimum, that is, we can consider a line with a slope of k from the bottom up, and set it to contact the first point is (XP,YP), p is the best decision point, so the best decision point of the set constitutes a lower convex package shape. Also because the slope K is 2*sum[i], as I increments, the slope k is incremented, so the points in the convex hull are monotonic and can be optimized using a monotone queue.
HDU3507 Print artical