dp/slope optimization
Slope optimization of the bare question ...
Sigh ... And the $10^6$ as 10W ... Re has n-fat
The problem is still very water.
Of course, in reverse order can also do ... But the whole thing turned out better.
Reversal of the a[0]= before the reversal of the a[n], and so on until A[n-1], inverted a[n]=0;
F[0]=a[0], because the first guard tower must be selected in the initial state.
Then define F[I] to show the guard tower at position I, 0~i the minimum value of the cost
Easy to get $f[i]=min\{f[j]+\frac{(i-j) * (i-j-1)}{2}+a[i] \} $
Monotonic Proof: $ (J > K) $
\[\begin{aligned} f[j]-f[k]+\frac{(i-j) * (i-j-1)}{2}-\frac{(i-k) * (i-k-1)}{2} &< 0 \ \ f[j]-f[k]+\frac{j^2+j-k^ 2-K}{2} &< i* (j-k) \ \frac{f[j]-f[k]+\frac{j^2+j-k^2-k}{2}}{j-k} &< i \end{aligned} \]
1 /**************************************************************2 problem:31563 User:tunix4 language:c++5 result:accepted6 time:2476 Ms7 memory:24708 KB8 ****************************************************************/9 Ten //Bzoj 3156 One#include <cstdio> A#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) the #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back - using namespacestd; + intGetint () { - intv=0, sign=1;CharCh=GetChar (); + while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} A while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} at returnv*Sign ; - } - Const intn=1000010; -typedefLong LongLL; - /******************tamplate*********************/ - LL n,q[n],l,r; in LL A[n],f[n]; -InlineDoubleSlop (LL k,ll j) { to return Double(f[j]-f[k]+ (J*J+J-K*K-K)/2.0)/Double(J-k); + } - intMain () { then=getint (); *D (i,n-1,0) a[i]=getint (); $f[0]=a[0];Panax NotoginsengF (I,1, N) { - while(L<r && Slop (q[l],q[l+1]) <i) l++; the intt=Q[l]; +f[i]=f[t]+ (LL) (i-t) * (i-t-1)/2+A[i]; A while(L<r && Slop (q[r-1],q[r]) >slop (q[r],i)) r--; theq[++r]=i; + } -printf"%lld\n", F[n]); $ return 0; $}View Code
"Bzoj" "3156" defense readiness