Topic links
Slope-Optimized n^2 DP
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 intYP ()5 {6 intx=0, f=0;CharCh=GetChar ();7 while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();}8 while(ch>='0'&&ch<='9') x*=Ten, x+=ch-'0', ch=GetChar ();9 return!f?x:-x;Ten } One intN,l=1, r,q[1000005]; A Long Longa[1000005],b[1000005],tot,ans,h[1000005],sum[1000005]; - DoublejsintAintb) - { the return(1. * (H[b]-h[a))/(1. * (A-b)); - } - intMain () - { +n=YP (); - for(intI=1; i<=n;i++) a[i]=YP (); + for(intI=1; i<=n;i++) B[i]=yp (), sum[i]=sum[i-1]+B[i]; A for(intI=1; i<n;i++) tot+=b[i]* (ni); attot+=a[n],q[++r]=N; - for(inti=n-1; i;i--) - { - while(L<r&&js (q[l],q[l+1]) >sum[i]) l++; - intj=Q[l]; -h[i]=h[j]+sum[i]* (J-i)-A[i]; inans=Max (ans,h[i]); - while(L<r&&js (Q[r],i) >js (q[r-1],Q[R]) r--; toq[++r]=i; + } -printf"%lld", tot-ans); the return 0; *}
bzoj3437 Little P's Ranch