BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 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+5,INF=1e9; typedef long long ll; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; 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)(2*a*(s[k]-s[j])); } int q[N],head,tail; void dp(){ head=tail=1; for(int i=1;i<=n;i++){ while(head<tail&&slope(q[head],q[head+1])<=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-1],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=1;i<=n;i++) s[i]=s[i-1]+read(); dp(); }
Copyright:http://www.cnblogs.com/candy99/