BZOJ 1096

const maxm=1e100; 

      maxn=1000001; 

var   f,x,p,c,sum,cost:array[0..maxn] of int64; 

      q:array[0..maxn] of longint; 

      n,i,h,t:longint; 

   

function calc(j,i:longint):int64; 

begin

   calc:=cost[i]-cost[j]-sum[j]*(x[i]-x[j]); 

end; 

   

function slope(j,k:longint):extended; 

var tmp:int64; 

begin

  tmp:=f[k]-f[j]-cost[k]+cost[j]+sum[k]*x[k]-sum[j]*x[j]; 

  if sum[k]=sum[j] then

   if tmp>=0 then exit(maxm) 

    else exit(-maxm); 

   slope:=tmp/(sum[k]-sum[j]); 

end; 

   

begin

  readln(n); 

  for i:=1 to n do readln(x[i],p[i],c[i]); 

  for i:=1 to n do sum[i]:=sum[i-1]+p[i]; 

  for i:=1 to n do

     cost[i]:=cost[i-1]+sum[i-1]*(x[i]-x[i-1]); 

  for i:=1 to n do

    begin

      while (h<t)and(slope(q[h],q[h+1])<x[i]) do inc(h); 

      f[i]:=f[q[h]]+calc(q[h],i)+c[i]; 

      while (h<t)and(slope(q[t-1],q[t])>slope(q[t],i)) do dec(t); 

      inc(t); q[t]:=i; 

    end; 

      writeln(f[n]); 

end.

 

你可能感兴趣的:(ZOJ)