算法:DP
分析:搞了半个上午的这道题,然后查知识发现是斜率优化,弄了半天还是弄不懂。
不过还算有一些收获……
分析:搞了半个上午的这道题,然后查知识发现是斜率优化,弄了半天还是弄不懂。
不过还算有一些收获……
program storage;
const
maxn=1000001;
var
n,i,j,h,t:longint;
f,s,y,p,x,w,c:array[0..maxn] of int64;
q:array[0..maxn] of longint;
function pan(j,k:longint):boolean;
begin
if y[j]-y[k]+x[i]*(w[j]-w[k])>=0 then exit(true) else exit(false);
end;
function cha(i,j,k:longint):boolean;
begin
if (y[j]-y[i])*(w[k]-w[j])-(y[k]-y[j])*(w[j]-w[i])<=0 then exit(true) else exit(false);
end;
begin
assign(input,'storage.in'); reset(input);
assign(output,'storage.out'); rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(x[i],p[i],c[i]);
p[i]:=p[i-1]+p[i];
s[i]:=s[i-1]+p[i-1]*(x[i]-x[i-1]);
end;
for i:=1 to n do
begin
while (h<t) and (pan(q[h],q[h+1])) do inc(h);
j:=q[h];
f[i]:=f[j]+s[i]-s[j]-p[j]*(x[i]-x[j])+c[i];
y[i]:=f[i]-s[i]+p[i]*x[i];
w[i]:=-p[i];
while (h<t) and (cha(q[t-1],q[t],i)) do dec(t);
inc(t);
q[t]:=i;
end;
writeln(f[n]);
close(input); close(output);
end.