4
-1 10 -20
2 2 3 4
标签:lin char desc div esc jpg turn math online
#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(); }
BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
标签:lin char desc div esc jpg turn math online
原文地址:http://www.cnblogs.com/candy99/p/6260180.html