题目大意:有n个物体,大小为$c_i$。把第i个到第j个放到一起,容器的长度为$x=j-i+\sum\limits_{k-i}^{j} c_k$,若长度为x,费用为$(x-L)^2$。费用最小.
题解:
$$令:a_i=\sum\limits_{i=1}^{i} c_i$$
$$dp_i=min(dp_j+(a_i+i-a_j-j-L-1)^2)$$
$$(以下称两点斜率为 slope(A,B) )$$
$$令:b_j=a_i+i,d_i=b_i+i+L+1$$
$$\therefore dp_i=dp_j+(b_i-d_j)^2$$
$$展开得:2a_i \cdot b_j+dp_i-a_i^2=dp_j+b_j^2$$
$$令:x_i=2b_i,y_i=dp_i+2b_i^2$$
斜率优化
卡点:无
C++ Code:
#include<cstdio>
using namespace std;
long long c[50010],f[50010],n,l;
int q[50010],h,t,tmp;
long long pw(long long i){return i*i;}
long long getb(int i){return c[i]+i;}
long long getd(int i){return getb(i)-l-1;}
long long getx(int i){return getb(i)*2;}
long long gety(int i){return f[i]+pw(getb(i));}
double slope(int a,int b){
return double(gety(a)-gety(b))/double(getx(a)-getx(b));
}
int main(){
scanf("%lld%lld",&n,&l);
for (int i=1;i<=n;i++)scanf("%lld",&c[i]),c[i]+=c[i-1];
for (int i=1;i<=n;i++){
while (h<t&&slope(q[h],q[h+1])<=getd(i))h++;
tmp=q[h];
f[i]=f[tmp]+pw(getd(i)-getb(tmp));
while (h<t&&slope(q[t-1],q[t])>=slope(q[t],i))t--;
q[++t]=i;
}
printf("%lld\n",f[n]);
return 0;
}