您的位置:首页 > 其它

bzoj 1010: [HNOI2008]玩具装箱toy 动态规划+斜率优化

2016-05-24 17:03 393 查看
一开始一直wa,后来才发现是精度问题,把乘法改成除法就A了。

代码:

const
maxn=60000;

var
n:longint;
l:int64;
f,s,a:array[0..maxn] of int64;
q:array[0..maxn] of longint;

procedure init;
var
i:longint;
begin
readln(n,l);
for i:=1 to n do
readln(a[i]);
for i:=1 to n do
s[i]:=s[i-1]+a[i];
for i:=1 to n do
s[i]:=s[i]+i;
end;

function get1(j,k:longint):int64;
begin
get1:=f[j]-f[k]+(s[j]+l)*(s[j]+l)-(s[k]+l)*(s[k]+l);
end;

function get2(j,k:longint):int64;
begin
get2:=2*s[j]-2*s[k];
end;

procedure main;
var
head,tail,i,x,y,z:longint;
begin
head:=1;
tail:=1;
q[1]:=0;
inc(l);
for i:=1 to n do
begin
while head<tail do
begin
x:=q[head+1]; y:=q[head];
if get1(x,y)/get2(x,y)<s[i]
then inc(head)
else break;
end;
x:=q[head];
f[i]:=f[x]+(s[i]-s[x]-l)*(s[i]-s[x]-l);
while head<tail do
begin
x:=i; y:=q[tail]; z:=q[tail-1];
if get1(x,y)/get2(x,y)<=get1(y,z)/get2(y,z)
then dec(tail)
else break;
end;
inc(tail);
q[tail]:=i;
end;
writeln(f
);
end;

begin
init;
main;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: