标签:
借着这个题学习了一下dp的斜率优化,感觉这东西好神啊。。。
先说一下裸的O(
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long f[50001],xi[50001],s[50001],queue[50001],L;
double g(int i,int j)
{
return (f[j]-f[i]+(s[j]+L)*(s[j]+L)-(s[i]+L)*(s[i]+L))/(2.0*(s[j]-s[i]));
}
int main()
{
int i,x,n,head,tail;
scanf("%d%d",&n,&L); L++;
xi[0]=s[0]=0; f[0]=0;
for (i=1;i<=n;++i)
{
scanf("%d",&x);
xi[i]=xi[i-1]+x;
s[i]=xi[i]+i;
}
head=1; tail=1; queue[tail]=0;
for (i=1;i<=n;++i)
{
while (head<tail&&g(queue[head],queue[head+1])<s[i]) head++;
f[i]=f[queue[head]]+(s[i]-s[queue[head]]-L)*(s[i]-s[queue[head]]-L);
while (head<tail&&g(queue[tail-1],queue[tail])>g(queue[tail],i)) tail--;
queue[++tail]=i;
}
printf("%I64d\n",f[n]);
}
【BZOJ 1010】【HNOI 2008】玩具装箱 toy
标签:
原文地址:http://blog.csdn.net/lcomyn/article/details/45974449