bzoj1010: [HNOI2008]玩具装箱toy
复习斜率优化。
#include<cstdio> #include<iostream> #include<cstring> using namespace std; typedef long long LL; LL s[51000],f[51000]; int list[51000]; double X(int j) { return double(f[j]+s[j]*s[j]); } double Y(int j) { return double(s[j]); } double slope(int j1,int j2) { return (X(j1)-X(j2)) / (Y(j1)-Y(j2)); } int main() { LL n,L,x; scanf("%lld%lld",&n,&L);L++; s[0]=0LL;for(int i=1;i<=n;i++) { scanf("%lld",&x); s[i]=s[i-1]+x+1; } int head=1,tail=1;list[1]=0; for(int i=1;i<=n;i++) { while( head<tail && slope(list[head],list[head+1]) <= double(2*(s[i]-L)) )head++; int j=list[head]; f[i]=f[j]+(s[i]-s[j]-L)*(s[i]-s[j]-L); while( head<tail && slope(list[tail-1],list[tail]) > slope(list[tail],i) )tail--; list[++tail]=i; } printf("%lld\n",f[n]); return 0; }
pain and happy in the cruel world.