BZOJ 1010: [HNOI2008]玩具装箱toy【斜率优化】

xiaoxiao2021-02-28  97

Description

 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京。他使用自己的压缩器进行压 缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中。P教授有编号为1…N的N件玩具,第i件玩具经过 压缩后变成一维长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的。同时如果一个一维容 器中有多个玩具,那么两件玩具之间要加入一个单位长度的填充物,形式地说如果将第i件玩具到第j个玩具放到一 个容器中,那么容器的长度将为 x=j-i+Sigma(Ck) i<=K<=j 制作容器的费用与容器的长度有关,根据教授研究, 如果容器长度为x,其制作费用为(X-L)^2.其中L是一个常量。P教授不关心容器的数目,他可以制作出任意长度的容 器,甚至超过L。但他希望费用最小.

题解

裸的斜率优化。

代码

#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 50006 #define LL long long using namespace std; inline char nc(){ static char buf[100000],*i=buf,*j=buf; return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++; } inline int _read(){ char ch=nc();int sum=0; while(!(ch>='0'&&ch<='9'))ch=nc(); while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc(); return sum; } struct point{ LL x,y; point operator -(const point&b)const{point c;c.x=x-b.x;c.y=y-b.y;return c;} LL operator *(const point&b)const{return x*b.y-y*b.x;} }que[maxn]; int n,L; LL A[maxn],C[maxn],f[maxn],sum[maxn]; int main(){ freopen("toys.in","r",stdin); freopen("toys.out","w",stdout); n=_read();L=_read(); for(int i=1,x;i<=n;i++)x=_read(),sum[i]=sum[i-1]+x,A[i]=sum[i]+i,C[i]=sum[i]+i-1-L; int hed=1,tal=1;que[1].x=2*A[0];que[1].y=A[0]*A[0]; for(int i=1;i<=n;i++){ point x;x.x=1;x.y=C[i]; while(hed<tal&&(que[hed+1]-que[hed])*x>=0)hed++; f[i]=que[hed].y+C[i]*(C[i]-que[hed].x);x.y=f[i]+A[i]*A[i];x.x=2*A[i]; while(hed<tal&&(x-que[tal])*(que[tal]-que[tal-1])>=0)tal--; que[++tal]=x; } printf("%lld\n",f[n]); return 0; }
转载请注明原文地址: https://www.6miu.com/read-59350.html

最新回复(0)