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\n",f[n]);
    return 0;
}

转载于:https://www.cnblogs.com/invoid/p/5527984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值