码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1096

时间:2014-10-27 19:12:16      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   for   sp   div   

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.

 

BZOJ 1096

标签:style   blog   io   color   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/logichandsome/p/4054665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!