/* I will wait for you */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#define make(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int maxn=1000010;
const int maxm=1000010;
const int maxs=26;
const int INF=1<<29;
const int P=1000000007;
const double error=1e-9;
LL a,b,c,x[maxn],q[maxn],f[maxn],h[maxn],s[maxn];
LL F(LL x)
{
return a*x*x+b*x+c;
}
LL A(int x,int y)
{
return (h[x]-f[x]+2*a*s[x]*s[x])-(h[y]-f[y]+2*a*s[y]*s[y]);
}
LL B(int x,int y)
{
return (2*a*s[x])-(2*a*s[y]);
}
int main()
{
int n;scanf("%d%lld%lld%lld",&n,&a,&b,&c);
for(int i=1;i<=n;i++) scanf("%lld",&x[i]);
for(int i=1;i<=n;i++) s[i]=s[i-1]+(LL)x[i],f[i]=F(s[i]);
int he=0,ta=0;f[0]=c;
for(int i=1,t;i<=n;i++)
{
while(he<ta&&A(q[he],q[he+1])<=B(q[he],q[he+1])*s[i]) he++;
t=q[he],h[i]=h[t]+F(s[i]-s[t]);
while(he<ta&&A(q[ta-1],q[ta])*B(q[ta],i)>=A(q[ta],i)*B(q[ta-1],q[ta])) ta--;
q[++ta]=i;
}
printf("%lld\n",h[n]);
return 0;
}
BZOJ1911【斜率优化】
最新推荐文章于 2015-12-03 21:54:08 发布