BZOJ1911【斜率优化】

/* 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值