Question address: http: // www. lydsy. com/judgeonline/problem. php? Id = 1010
For more information, see the original question.
Algorithm analysis:
If s [I] is set to the prefix and f [I] of C [I], it indicates the minimum cost from 1st items to I items.
The easy-to-obtain DP equation is f [I] = min (F [J] + (s [I]-s [J] + I-j + 1-L) ^ 2 ).
If t [I] = s [I] + I is set, F [I] = min (F [J] + (T [I]-T [J] + 1-L) ^ 2.
If M = T [I]-L-1, F [I] = min (F [J] + (m-T [J]) ^ 2 ).
When making a decision on I, set J and K to two general decision points. Set j <K.
If K is better than J
F [k] + (m-T [k]) ^ 2 <F [J] + (m-T [J]) ^ 2
Expand, get
F [k] + m ^ 2-2 * m * t [k] + T [k] ^ 2 <F [J] + m ^ 2-2 * m * t [j] + T [J] ^ 2
Simplified
(F [k] + T [k] ^ 2)-(F [J] + T [J] ^ 2 )) /(t [k]-T [J]) <2 * m = 2 * (s [I] + i-l-1)
Therefore, we can use monotonous queues to optimize the DP.
Pseudo code of the monotonous queue-optimized DP:
for (int i=1;i<=n;++i){while (h+1<t && slope(q[h],q[h+1])<...) h++;f[i]=...;while (h+1<t && slope(q[t-2],q[t-1])>slope(q[t-1],i)) t--;q[t++]=i;}
Two important conclusions of slope optimization (for this question ):
1) j <K, if slope (j, k) <2 * (s [I] + i-l-1), k is better than J. (PS: The left side of the inequality is the formula child related only to J and K, and the right side of the inequality is the formula child related only to I)
2) j <k <L. If slope (j, k)> slope (K, L), K can be removed.
Code: [to be added]
By Charlie pan
Jul 18,2014