复习斜率优化。。

显然得到dp方程为

f[i]=min{f[j]+(i-j-1+b[i]-b[j]-l)^2} j=1..i-1

其中b[i]为a[i]的前缀和。。

然后复杂度n^2,要优化。。

直接取j,k两点作差得。。当j比k优时

(f[j]-f[k])/(j-k+b[j]-b[k])+b[j]+b[k]+j+k<2*i+2*b[i]-2-2*L

然后用单调队列维护下凸包即可。。。



/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 50005
#define nm 100005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



int n,l,q[NM],qh,qt;
ll f[NM],b[NM];
double slope(int x,int y){return 1.0*(f[x]-f[y])/(x-y+b[x]-b[y])+b[x]+b[y]+x+y;}

int main(){
n=read();l=read();
inc(i,1,n)b[i]=b[i-1]+read();
qh=qt=1;
inc(i,1,n){
while(qh<qt&&slope(q[qh],q[qh+1])<2*i+2*b[i]-2-2*l)qh++;
//printf("%d\n",q[qh]);
f[i]=f[q[qh]]+sqr(i-q[qh]-1+b[i]-b[q[qh]]-l);
while(qh<=qt&&slope(q[qt-1],q[qt])>slope(q[qt],i))qt--;
q[++qt]=i;
}
return 0*printf("%lld\n",f[n]);
}




1010: [HNOI2008]玩具装箱toy

Time Limit: 1 Sec   Memory Limit: 162 MB

Submit: 12676  

Solved: 5551

[​Submit​​][​Status​​][​Discuss​​]


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。但他希望费用最小.

Input

  第一行输入两个整数N,L.接下来N行输入Ci.1<=N<=50000,1<=L,Ci<=10^7

Output

  输出最小费用

Sample Input

5 4
3
4
2
1
4

Sample Output

1

HINT

Source[ ​​Submit​][​Status​][​Discuss​​]