Foreword
This is what I wrote the first line of $ dp $ slope optimized title, $ dp $'ve always vegetables, coffee and chicken say this is the basic things, but look at the others to optimize the abundant formulas for $ dp $ slopes and honestly do not understand a few questions, the more concrete
description
N-$ and $ given $ l $. Toy has $ n-$, $ I $ length of the toy is $ c [i] $, the toy requires divided into several segments, from the $ I $ $ $ into J segment length $ x = j-i + \ sum_ {k = i} ^ {j} c [k] $, costs $ (xl) ^ {2} $. seeking the minimum cost [Link]
analysis
With $ dp [i] $ represents the minimum cost of a toy before i, there
$dp[i]=min\left \{ dp[j]+(sum[i]-sum[j]+i-(j+1)-l)^{2}(1<=j<i) \right \}$
Wherein $ sum [i] $ represents the $ c [i] $ prefix and.
For convenience, we set up
$A[i]=sum[i]+i,l=l+1$
Then the original equation is equivalent to
$dp[i]=min\left\{dp[j]+(A[i]−A[j]−l)^{2}(1<=j<i)\right\}$
We set $ j <k <i $ and in the calculation of $ dp [i] $ when k better decisions. That
$dp[k]+(A[i]−A[k]−l)^{2}<dp[j]+(A[i]−A[j]−l)^{2}$
To write or draw on the paper, then the formula becomes what shaped opening can be easily obtained
$\frac{[dp[k]+(A[k]+l)^{2}]-[dp[j]+(A[j]+l)^{2}]}{2\times A[k]-2\times A[j]}$ $<A[i]$
Is not like
$ \ frac {Y_ {k}
-Y_ {j}} {X_ {k} -X_ {j}} $ form?
This stuff is not that slope it?! We set it to $ g (k, j) $
We can find $ A [i] $ is monotonically increasing, so all decisions can be converted into a set of points on the two-dimensional space.
That slope $ k $ j at this point and this connection point is less than $ A [i] $, $ k $ then this decision is even more preferred.
Then for three policymaking $ a <b <c $, if $ g (c, b) <= g (b, a) $, $ b $ then the decision will not be selected. Why? Let's talk about (for any $ 3 <i <= n $):
1. If $ g (b, a) <A [i] $, then there must be $ g (c, b) <A [i] $, $ c $ is optimal, $ c $ selection decisions.
2. If $ g (b, a)> = A [i] $, $ b $ then not optimal, optimal, or may be $ A $ $ c $.
So we added a new point of time, you can see it as a $ c $, then all such $ b $ are removed until the $ g (c, b)> g (b, a) $, so we the slope to be addressed is monotonically increasing.
So that we can maintain a monotonous queue are the first team and the tail friends.
Code
#include <cstdio> #define ll long long #define Empty (head>=tail) const int maxn = 5e4+10; ll n, L, head, tail, j; ll Q[maxn], sum[maxn], s[maxn], f[maxn]; inline double X(ll i) {return s[i];} inline double Y(ll i) {return f[i]+(s[i]+L-1)*(s[i]+L-1);} inline double Rate(ll i,ll k) {return (Y(k)-Y(i))/(X(k)-X(i));} int main() { scanf("%lld%lld", &n, &L); for (int i = 1; i <= n; i++) { scanf("%lld", &sum[i]); sum[i] += sum[i-1], s[i] = sum[i]+i; } head = tail = 1; Q[1] = 0; for (int i = 1; i <= n; i++) { while(!Empty&&Rate(Q[head],Q[head+1])<2*s[i]) head++; j = Q[head]; f[i] = f[j]+(s[i]-s[j]-L-1)*(s[i]-s[j]-L-1); while(!Empty&&Rate(Q[tail-1],Q[tail])>Rate(Q[tail],i)) tail--; Q[++tail] = i; } printf("%lld\n", f[n]); }
网上讲了很多数形结合的方法,找截距最小,的确对理解很有帮助,但是可能像这样的对我来说更好理解,另外有些细枝末节的东西没有完全看懂,但是现在没必要纠结.
这里能用斜率优化是因为A[i]是单调的,至于具体为什么,先不细究
参考文章:
https://www.cnblogs.com/Sunnie69/p/5575464.html
https://www.cnblogs.com/terribleterrible/p/9669614.html
https://www.cnblogs.com/Paul-Guderian/p/7259491.html