#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn (500000+10)
int n,m;
int dp[maxn],sum[maxn],q[maxn];
int up(int xx,int yy){//xx<yy
return dp[yy]+sum[yy]*sum[yy]-dp[xx]-sum[xx]*sum[xx];
}
int down(int xx,int yy){//xx<yy
return 2*(sum[yy]-sum[xx]);
}
void input()
{
while(scanf("%d%d",&n,&m)==2){
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++){
scanf("%d",&sum[i]);
sum[i]+=sum[i-1];
}
int tail=0,head=0;
q[tail++]=0;
for(int i=1;i<=n;i++){
while(head<tail-1 && up(q[head],q[head+1])<=sum[i]*down(q[head],q[head+1]))head++;
int temp=q[head];
dp[i]=dp[temp]+(sum[i]-sum[temp])*(sum[i]-sum[temp])+m;
while(head<tail-1&&up(q[tail-1],i)*down(q[tail-2],q[tail-1])<=up(q[tail-2],q[tail-1])*down(q[tail-1],i))
tail--;
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
}
int main()
{
input();
return 0;
}
hdu 3507(斜率优化)
最新推荐文章于 2020-05-21 21:25:44 发布