DP+斜率优化

就不再多说了。。。还是基础的斜率优化。。。

 

  • var

  • n,i,s,t,a,b,c,xa,xb,xc:longint;

  • num,dp,xl,sum,s2:array[0..1000005] of int64;

  •  

  • begin

  • read(n,xa,xb,xc);

  • for i:=1 to n do read(num[i]);

  • for i:=1 to n do sum[i]:=sum[i-1]+num[i];

  • s:=1;t:=1;

  • for i:=1 to n do

  •   begin

  •   while s<t do

  •     begin

  •     a:=xl[s];

  •     b:=xl[s+1];

  •     if 2*xa*sum[i] < (s2[b]-s2[a])/(sum[b]-sum[a]) then

  •       begin

  •       xl[s]:=0;inc(s);

  •       end

  •     else break;

  •     end;

  •   a:=xl[s];

  •   dp[i]:=dp[a]+xa*sqr(sum[i]-sum[a])+xb*(sum[i]-sum[a])+xc;

  •   s2[i]:=dp[i]+xa*sqr(sum[i])-xb*sum[i];

  •   inc(t);

  •   xl[t]:=i;

  •   while t-s>1 do

  •     begin

  •     a:=xl[t-2];b:=xl[t-1];c:=xl[t];

  •     if (s2[b]-s2[a])/(sum[b]-sum[a]) < (s2[b]-s2[c])/(sum[b]-sum[c]) then

  •       begin

  •       xl[t-1]:=xl[t];xl[t]:=0;dec(t);

  •       end

  •     else break;

  •     end;

  •   end;

  • write(dp[n]);

  • end.

 

相关文章:

  • 2021-09-25
  • 2022-01-13
  • 2022-12-23
  • 2021-08-21
  • 2021-07-19
  • 2021-07-29
  • 2021-12-19
  • 2022-12-23
猜你喜欢
  • 2021-05-20
  • 2021-06-04
  • 2021-12-08
  • 2021-07-08
  • 2021-12-09
  • 2022-01-03
相关资源
相似解决方案