bzoj 1911 特别行动队
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1911
Description
![](http://www.lydsy.com/JudgeOnline/images/1911_1.jpg)
Input
![](http://www.lydsy.com/JudgeOnline/images/1911_2.jpg)
Output
![](http://www.lydsy.com/JudgeOnline/images/1911_3.jpg)
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
Apio居然这么裸的斜率优化.......
我们设k比j更优:
dp[k]+a(s[i]-s[k])^2+b(s[i]-s[k])<dp[j]+a(s[i]-s[j])^2+b(s[i]-s[j])
dp[k]-dp[j]+as[k]^2-as[j]^2+b(s[j]-s[k])<2as[i](s[k]-s[j])
#include<cstdio>
#include<iostream>
#define MAXN 1000005
using namespace std;
typedef long long ll;
int n;
ll a,b,c,h,t,x;
ll sum[MAXN];
ll f[MAXN];
int q[MAXN];
ll get(int j,int k)
{
return f[j]-f[k]+a*(sum[j]*sum[j]-sum[k]*sum[k])-b*(sum[j]-sum[k]);
}
int main()
{
scanf("%d%lld%lld%lld",&n,&a,&b,&c);
for(int i=1; i<=n; i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1];
}
f[0]=0;
q[0]=0;
t=h=0;
for(int i=1; i<=n; i++)
{
while(h<t&&get(q[h],q[h+1])<=2*a*sum[i]*(sum[q[h]]-sum[q[h+1]])) h++;
x=sum[i]-sum[q[h]];
f[i]=f[q[h]]+a*x*x+b*x+c;
while(h<t&&get(q[t-1],q[t])*(sum[q[t]]-sum[i]) <=get(q[t],i)*(sum[q[t-1]]-sum[q[t]]))
t--;
q[++t]=i;
}
printf("%lld",f[n]);
return 0;
}