bzoj1911: [Apio2010]特别行动队

斜率优化。

大水题。。。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000000 + 10;

int q[maxn],l,r;
long long f[maxn],s[maxn],a,b,c,x;
int n,m;

inline long long sqr(long long x) {
    return x*x;
}

double slop (int k,int j) {
    return (f[j]-f[k]+a*(sqr(s[j])-sqr(s[k]))-b*(s[j]-s[k])) / (2.0*a*(s[j]-s[k]));
}    



int main() {
    scanf("%d%lld%lld%lld",&n,&a,&b,&c);
    for(int i=1;i<=n;i++) {
        scanf("%d",&s[i]);
        s[i]+=s[i-1];
    }
    
    q[l=r=0]=0;
    for(int i=1,t;i<=n;i++) {
        while(l<r && slop(q[l],q[l+1]) < s[i]) l++;    
        t=q[l];
        x=(s[i]-s[t]);
        f[i]=f[t]+a*sqr(x)+b*x+c;
        while(l<r && slop(q[r-1],q[r])>slop(q[r],i)) r--;
        q[++r]=i;
    }
    printf("%lld
",f[n]);
    return 0;
}
原文地址:https://www.cnblogs.com/invoid/p/5527984.html