bzoj1010: [HNOI2008]玩具装箱toy
这种题自己讲不清楚....比较蒟蒻 还是推荐一篇博客吧 http://hzwer.com/2114.html
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int M=50007; int read(){ int ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();} return ans*f; } int n,l; LL s[M],f[M],c; int q[M],w[M]; double slop(int j,int k){ return (f[k]-f[j]+(s[k]+c)*(s[k]+c)-(s[j]+c)*(s[j]+c))/(2.0*(s[k]-s[j])); } int main() { n=read(); l=read(); c=l+1; for(int i=1;i<=n;i++) w[i]=read(),s[i]=s[i-1]+w[i]; for(int i=1;i<=n;i++) s[i]+=i; int head=1,tail=1; q[tail]=0; for(int i=1;i<=n;i++){ while(head<tail&&slop(q[head],q[head+1])<=s[i]) head++; int v=q[head]; f[i]=f[v]+(s[i]-s[v]-c)*(s[i]-s[v]-c); while(head<tail&&slop(q[tail],i)<slop(q[tail-1],q[tail])) tail--; q[++tail]=i; } printf("%lld\n",f[n]); return 0; }