BZOJ 1096

本文深入探讨了高效算法的设计与实现过程,通过具体实例讲解如何优化算法效率,减少计算复杂度,提高程序运行速度。文章详细介绍了算法设计的基本原则,算法优化技巧,以及在实际编程中如何应用这些理论知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

转载于:https://www.cnblogs.com/logichandsome/p/4054665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值