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.
代码:
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.
相关文章推荐
- 修改dbname和sid
- JSON解析
- centos 安装 Sublime Text
- 浏览器PlaceHolder效果
- HTML+CSS基础(六):CSS样式与属性
- 图片位图颜色反转
- CSDN、Github等优秀资源(持续更新)
- mysql innodb 表数据压缩
- 箱排序(Bin Sort)
- 用Ngen指令加快C#程序的启动速度
- 模块对于 SAFESEH 映像是不安全的。
- android handler机制学习
- hdu 2833(Floyd + dp)
- 蓝桥杯 密文搜索
- 用python扩展包matplotlib画图
- ubuntu下配置samba服务与虚拟机下的win7实现文件共享(局域网也可共享)
- java同步锁(synchronized)
- 【47】java的类之间的关系:泛化、依赖、关联、实现、聚合、组合
- framework、.a封装
- 【47】java的类之间的关系:泛化、依赖、关联、实现、聚合、组合