HNOI2008玩具装箱 (斜率优化)

时间:2022-06-20 00:44:28

总算A了,心情好激动……

如果会了一类斜率优化,基本上这类题就成了套模版了……

只是k函数不同

 var n,l,x,tail,head,m:int64;
i,j:longint;
dp,q,s:array[..] of int64;
function k(x,y:longint):double;
begin
k:=1.0*((dp[x]+s[x]*s[x]-dp[y]-s[y]*s[y])/(s[x]-s[y]));
end;
procedure main;
begin
readln(n,l);s[]:=;
for i:= to n do
begin
readln(x);
s[i]:=s[i-]+x;
end;
for i:= to n do s[i]:=s[i]+i;
head:=;tail:=;
for i:= to n do
begin
m:=s[i]-l-;
while (head<tail) and (k(q[head+],q[head])<=*m) do inc(head);
j:=q[head];
dp[i]:=dp[j]+(m-s[j])*(m-s[j]);
while (head<tail) and (k(q[tail],q[tail-])>=k(i,q[tail])) do dec(tail);
inc(tail);q[tail]:=i;
end;
writeln(dp[n]);
end;
begin
main;
end.

还有一份写得很不错的题解:

http://www.cnblogs.com/perseawe/archive/2012/05/12/BZ1010.html