dp/slope optimization
Well...... The third slope optimization problem.
Definition $s [i]=\sum_{k=1}^{i} X[k] $
Equation: $f [i]=max\{f[j]+a* (S[i]-s[j]) ^2+b* (s[i]-s[j]) +c \} $
For $ j > K $
If decision J is better than K: \[\begin{aligned} {f[j] + A * (S[i]-s[j]) ^2+b* (S[i]-s[j]) +c} &> {f[k]+a* (s[i]-s[k]) ^2+b* (S[i]-s[k]) +c } \ \ {f[j]-f[k]+a* (s[j]^2-s[k]^2)-b* (S[j]-s[k])} &> {2a*s[i]* (s[j]-s[k])} \ \ \frac{f[j]-f[k]+a* (s[j]^2-s[k]^2 )-b* (S[j]-s[k])} {2a* (S[j]-s[k])} &> {S[i]} \end{aligned} \]
1 /**************************************************************2 problem:19113 User:tunix4 language:c++5 result:accepted6 time:1260 Ms7 memory:28616 KB8 ****************************************************************/9 Ten //Bzoj 1911 One#include <cmath> A#include <vector> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #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 A using namespacestd; at intGetint () { - intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*=Sign ; - } in Const intn=1e6+Ten; -typedefLong LongLL; to /******************tamplate*********************/ + LL F[n],x[n],s[n]; - intq[n],a,b,c; the DoubleSlopintKintj) { * return Double(f[j]-f[k]+a* (S[j]*s[j]-s[k]*s[k]) +b* (s[k]-s[j])/ $ Double(2*a* (s[j]-s[k]));Panax Notoginseng } - intMain () { the #ifndef Online_judge +Freopen ("1911.in","R", stdin); AFreopen ("1911.out","W", stdout); the #endif + intn=getint (); -A=getint (), B=getint (), c=getint (); $F (I,1, n) {x[i]=getint (); s[i]=s[i-1]+x[i];} $ intL=0, r=0; -F (I,1, N) { - while(L<r && Slop (q[l],q[l+1]) <s[i]) l++; the intt=Q[l]; -F[i]=f[t]+a* ((S[i]-s[t]) * (s[i]-s[t)) +b* (S[i]-s[t]) +C;Wuyi while(L<r && Slop (q[r-1],q[r]) >slop (q[r],i)) r--; theq[++r]=i; - } Wuprintf"%lld\n", F[n]); - return 0; About}
View Code
"Bzoj" "1911" "APIO2010" Special Ops Team Commando