Slope DP CDQ Split Treatment

Source: Internet
Author: User

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&LT;T&AMP;&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.