[APIO2010] Special Operations team
The surface is very straightforward, will not let go.
Too much of a routine, do not feel a bit.
\ (DP (i) =DP (j) +a* (S (i)-S (j)) ^{2}+b* (S (i)-S (j)) +c\)
Direct introduction of a slope optimization equation on the monotone queue just fine
Time/Space complexity: \ (O (n) \)
#include <cstdio>#defineSid 1000500#defineRI Register int#definell Long Long#defineDD Doubleusing namespacestd;#defineGetChar () *s + +Charrr[30000005], *s =Rr;inlineintRead () {intp =0, W =1; Charc =GetChar (); while(C >'9'|| C <'0') { if(c = ='-') W =-1; C=GetChar (); } while(c >='0'&& C <='9') {p= P *Ten+ C-'0'; C=GetChar (); } returnP *W;} ll Dp[sid], Sum[sid];intQ[sid], N, a, b, C;#defineX (g) (sum[(g)])#defineY (g) (dp[(g)] + A * sum[(g)] * sum[(g)]-B * sum[(g)])inline DD s (intIintj) { return(DD) (Y (i)-y (j))/(DD) (x (i)-x (j));}intMain () {fread (RR,1,sizeof(RR), stdin); N= Read (); A =read (); b= Read (); c =read (); for(RI i =1; I <= N; i + +) sum[i] = sum[i-1] +read (); RI FR=1, to =1; for(RI i =1; I <= N; i + +){ while(Fr +1<= to && s (Q[FR],Q[FR +1]) >2* A * Sum[i]) fr + +; intp =Q[FR]; LL pp= Sum[i]-Sum[p]; Dp[i]= Dp[p] + A * pp * pp + b * pp +C; while(Fr +1<= to && S (q[to], q[to-1]) <= s (I, q[to-1]) to--; q[+ + to]=i; } printf ("%lld\n", Dp[n]); return 0;}
Special Operations Team
[APIO2010] Special ops team---slope optimization dp