1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 4142 Solved: 1964
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
f[i]=max{f[j]+...}
随便一化就好了
(a*(s[k]*s[k]-s[j]*s[j])+f[k]-f[j]+b*(s[j]-s[k])) / (2*a*(s[k]-s[j]))
最后是s[i]>=slope(j,k)时k优
s[]是单调的,用单调队列维护这个下凸壳
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e6+,INF=1e9;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,a,b,c;
ll s[N],f[N];
inline double slope(int j,int k){
return (double)(a*(s[k]*s[k]-s[j]*s[j])+f[k]-f[j]+b*(s[j]-s[k]))/(double)(*a*(s[k]-s[j]));
}
int q[N],head,tail;
void dp(){
head=tail=;
for(int i=;i<=n;i++){
while(head<tail&&slope(q[head],q[head+])<=s[i]) head++;
int j=q[head];
f[i]=f[j]+a*(s[i]-s[j])*(s[i]-s[j])+b*(s[i]-s[j])+c;//printf("f %lld %d\n",f[i],j);
while(head<tail&&slope(q[tail-],q[tail])>slope(q[tail],i)) tail--;
q[++tail]=i;
}
printf("%lld",f[n]);
}
int main(){
//freopen("in.txt","r",stdin);
n=read();a=read();b=read();c=read();
for(int i=;i<=n;i++) s[i]=s[i-]+read();
dp();
}