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
3
4
2
1
4
Sample Output
1
正解:划分型dp+斜率优化。
很容易想出n^2的dp,然后发现可以分成i的状态,j的状态和i*j的状态,那么这就可以用斜率优化做了。
//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
using namespace std;
struct node{ ll x,y; }q[50010];
ll f[50010],c[50010],n,l,st,ed;
il ll gll(){
RG ll x=0,q=0; RG char ch=getchar();
while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
}
il void work(){
n=gll(),l=gll()+1; for (RG ll i=1;i<=n;++i) c[i]=gll()+c[i-1]; for (RG ll i=1;i<=n;++i) c[i]+=i;
st=1,ed=2,f[1]=(c[1]-l)*(c[1]-l); q[ed]=(node){2*c[1],f[1]+c[1]*c[1]+2*l*c[1]};
for (RG ll i=2;i<=n;++i){
RG ll k=c[i]; while (st<ed && (q[st+1].y-q[st].y)<(q[st+1].x-q[st].x)*k) st++;
f[i]=(c[i]-l)*(c[i]-l)+q[st].y-k*q[st].x; RG ll X=2*c[i],Y=f[i]+c[i]*c[i]+2*l*c[i];
while (st<ed && (Y-q[ed].y)*(q[ed].x-q[ed-1].x)<(q[ed].y-q[ed-1].y)*(X-q[ed].x)) ed--; q[++ed]=(node){X,Y};
}
printf("%lld",f[n]); return;
}
int main(){
work();
return 0;
}