Topic links
Slope optimization
1#include <cstdio>2#include <cstdlib>3#include <string>4#include <cstring>5#include <cmath>6 #defineRe (I,L,R) for (int i= (l); i<= (R); i++)7 using namespacestd;8typedefLong LongLL;9Template<typename q>Ten voidInin (Q &x) One { Ax=0;intf=0;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();} - while(ch>='0'&&ch<='9') x= (x<<3) + (x<<1) +ch-'0', ch=GetChar (); thex=f?-x:x; - } - intn,a,b,c; -LL sum[1000010],x[1000010],y[1000010],aa[1000010],dp[1000010]; + intq[1000010]; -LL F (ConstLL &a) {returnA *A;} + intMain () A { at inin (n); Inin (a), Inin (b), Inin (c); -Re (I,1, N) inin (Aa[i]), sum[i]=sum[i-1]+Aa[i]; -Re (I,1, N) x[i]=1ll*a*Sum[i]; - intL=0, r=0; -Re (I,1, N) - { in while(l<r&&y[q[l+1]]-y[q[l]]>2ll*sum[i]* (x[q[l+1]]-X[Q[L]]) l++; - intt=Q[l]; toDp[i]=dp[t]+1ll*f (Sum[i]-sum[t]) *a+1ll* (sum[i]-sum[t]) *b+C; +Y[i]=dp[i]+1ll*a*f (Sum[i]) -1ll*b*Sum[i]; - while(l<r&& (y[q[r]]-y[q[r-1]]) * (X[i]-x[q[r]) > (Y[i]-y[q[r]) * (x[q[r]]-x[q[r-1]]) r--; theq[++r]=i; * } $printf"%lld", Dp[n]);Panax Notoginseng return 0; -}
bzoj1911 [Apio2010] Special Operations Team Commando