Solution:
- 线性DP
(打牌) - (dp)方程还是很好想的:(dp[i]=dp[j-1]+a*(s[i]-s[j-1])^2+b*(s[i]-s[j-1])+c)
- 我们假定(j<k),且令(f(j)=dp[j-1]+a*(s[i]-s[j-1])^2+b*(s[i]-s[j-1])+c)
- 可以列出式子$$f(j)<f(k)$$
即(下面这个太长了,自己写写看得清楚些)
[dp[j-1]+a*(s[i]-s[j-1])^2+b*(s[i]-s[j-1])+c<dp[k-1]+a*(s[i]-s[k-1])^2+b*(s[i]-s[k-1])+c
]
化简可得:
[frac{dp[j-1]-dp[k-1]+a*(s[j-1]^2-s[k-1]^2)-b*(s[j-1]-s[k-1])}{2*a*(s[j-1]-s[k-1])}>s[i]
]
- 然后维护一个下凸包就好了
Code:
//It is coded by Ning_Mew on 5.24
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e6+7;
int n;
LL a,b,c,s[maxn],ss=0,tt=1,team[maxn];
LL dp[maxn];
double slope(int j,int k){
return 1.0*(dp[j-1]-dp[k-1]+a*(s[j-1]*s[j-1]-s[k-1]*s[k-1])-b*(s[j-1]-s[k-1]))/(2*a*(s[j-1]-s[k-1]));
}
LL add(int i,int j){
LL x=s[j]-s[i-1];return x*x*a+x*b+c;
}
int main(){
scanf("%d",&n);
scanf("%lld%lld%lld",&a,&b,&c);
for(int i=1;i<=n;i++){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}
dp[1]=s[1]*s[1]*a+s[1]*b+c;
team[1]=1;ss=1;tt=2;
//cout<<"dp:"<<1<<' '<<dp[1]<<endl;
for(int i=2;i<=n;i++){
while(ss+1<tt&&slope(team[ss],team[ss+1])<s[i])ss++;
dp[i]=max( dp[team[ss]-1]+add(team[ss],i) , dp[i-1]+add(i,i) );
//cout<<"dp:"<<i<<' '<<dp[i]<<endl;
while(ss+1<tt&&slope(team[tt-1],i)<slope(team[tt-2],team[tt-1]))
tt--;
team[tt]=i;tt++;
}
printf("%lld
",dp[n]);
return 0;
}