F[i] = min {f[j] + SQR (A[i]-a[j])}
f[i]= min {-2 * a[i] * A[j] + a[j] * A[j] + f[j]} + a[i] * A[i]
Because A[i] is not monotonically incrementing, it cannot directly slope DP.
Consider having CDQ division to do, complexity (NLOG2N)
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 #defineMAXN 1000088 #defineLL Long Long9 Ten Long LongF[MAXN]; One intA[MAXN],B[MAXN]; A intN; - BOOLflag=1; - the voidReadint&x) { - Charch; - for(Ch=getchar ();ch<'0'|| Ch>'9'; Ch=getchar ()); x=ch- -; - for(Ch=getchar (); ch>='0'&&ch<='9'; Ch=getchar ()) x=x*Ten+ch- -; + } - + voidinit () { A read (n); at for(intI=1; i<=n;i++) {read (a[i]); Read (B[i]);if(B[i]) flag=0; } - for(intI=1; i<=n;i++) f[i]= (LL)1<< -; - } - - voidForce () { - for(intI=1; i<=n;i++) in for(intj=0; j<=i-1; j + +) - if(a[j]>=B[i]) toF[i]=min (f[i],f[j]+ (LL) (A[i]-a[j]) * (a[i]-a[j])); + - } the * intQ[MAXN],RK[MAXN]; $ BOOLcmpintIintj) {Panax Notoginseng returna[i]<A[j]; - } the + Long LongKxintIintj) { A return 2* (a[i]-a[j]); the } + - Long LongKyintIintj) { $ Long Longans=1ll*a[i]*a[i]+f[i]-1ll*a[j]*a[j]-F[j]; $ returnans; - } - the BOOLCMP1 (intIintJintk) { - returnKY (k,j) *kx (j,i) <=kx (k,j) *KY (j,i);Wuyi } the - BOOLCMP2 (intIintJintk) { Wu returnKY (I,J) >=k*kx (I,J); - } About $ voidSolveintLintR) { - if(L==R)return; - intMid= (l+r) >>1; - solve (l,mid); A for(inti=l;i<=r;i++) rk[i]=i; +Sort (rk+l,rk+r+1, CMP); the intH=1, t=0; - for(inti=l;i<=r;i++) $ { the if(rk[i]<=mid) { the while(H<t&&cmp1 (q[t-1],q[t],rk[i]) t--; theq[++t]=Rk[i]; the}Else { - while(H<T&&CMP2 (q[h],q[h+1],a[rk[i]]) h++; inF[rk[i]]=min (f[rk[i]],f[q[h]]+1ll* (A[rk[i]]-a[q[h]) * (a[rk[i]]-a[q[h])); the } the } AboutSolve (mid+1, R); the } the intMain () { the init (); + if(n<= +) force (); - if(flag) Solve (0, n); theprintf"%.4f", sqrt (f[n]));Bayi}
Slope DP CDQ partition