斜率优化dp,比较裸

注意int64的运算

 1 var f,a,q:array[0..2000010] of int64;
 2     i,n,h,t:longint;
 3     x,y,z:int64;
 4 
 5 function g(j,k:int64):double;
 6   var p,q:double;
 7   begin
 8     p:=2*(f[k]-f[j])+sqr(k)-sqr(j)-k+j;
 9     q:=2*(k-j);
10     exit(p/q);
11   end;
12 
13 begin
14   readln(n);
15   for i:=1 to n do
16     read(a[i]);
17   h:=0;
18   t:=0;
19   q[0]:=n;
20   f[n]:=a[n];
21   for i:=n-1 downto 0 do
22   begin
23     while (h<t) and (g(q[h],q[h+1])>i) do inc(h);
24     x:=q[h];
25     y:=i;
26     z:=(x-y)*(x-y-1);
27     f[i]:=(2*f[q[h]]+z) div 2+a[i];
28     if i<>0 then
29     begin
30       while (h<t) and (g(q[t],i)>g(q[t-1],q[t])) do dec(t);
31       inc(t);
32       q[t]:=i;
33     end;
34   end;
35   writeln(f[0]);
36 end.
View Code

 

posted on 2014-05-27 22:30  acphile  阅读(169)  评论(0编辑  收藏  举报