[斜率DP优化]BZOJ 1911: [Apio2010]特别行动队 题解

(传送门)

题目大意

给出一个有n个数的数组 ai ,要求将a分割成若干个区间,每个区间的价值是

令这个区间所有数的和为x,则该区间价值为A*x*x+B*x+C

一个分割方案ans=所有分割的区间的价值和,求最大的 ans

解题分析

明显的线性DP,直接斜率优化。

转移方程

f[i]=max{f[j]+A(sum[i]sum[j1])2+B(sum[i]sum[j1])+C}

直接贴最后的结果

f[j]+Asum[j]2f[k]+Asum[k]2sum[j]sum[k]>2Asum[i]+B

唯一的注意点在于这道题求得是max,符号别搞错。

第一次推得时候推错了,忘给 2Asum[i]sum[j] 配A这个系数,结果错的离谱,差了好久才发现开始就推错了……

神奇的Markdown

复杂度:
时间: O(n) ; 空间: O(n)

#include<cstdio>
#define LL long long
using namespace std;
int n,que[1000005],hed,til;
LL f[1000005],sum[1000005],A,B,C;
inline void readi(int &x){
    x=0; int f=1; char ch=getchar(),lst='+';
    while ('0'>ch||ch>'9') {lst=ch; ch=getchar();}
    if (lst=='-') f=-f;
    while ('0'<=ch&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    x*=f;
}
inline void readL(LL &x){
    x=0; int f=1; char ch=getchar(),lst='+';
    while ('0'>ch||ch>'9') {lst=ch; ch=getchar();}
    if (lst=='-') f=-f;
    while ('0'<=ch&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    x*=f;
}
LL getX(const int i,const int j){return sum[j]-sum[i];}
LL getY(const int i,const int j){return f[j]+A*sum[j]*sum[j]-f[i]-A*sum[i]*sum[i];}
int main()
{
    freopen("commando.in","r",stdin);
    freopen("commando.out","w",stdout);
    readi(n); readL(A); readL(B); readL(C);
    hed=til=1; f[0]=sum[0]=que[1]=0;
    for (int i=1,x;i<=n;i++){
        readi(x); sum[i]=sum[i-1]+x;
        while (hed<til&&getY(que[hed],que[hed+1])>=getX(que[hed],que[hed+1])*(2*A*sum[i]+B)) hed++;
        x=sum[i]-sum[que[hed]]; f[i]=f[que[hed]]+A*x*x+B*x+C;
        while (hed<til&&getY(que[til-1],que[til])*getX(que[til],i)<=getY(que[til],i)*getX(que[til-1],que[til])) til--;
        que[++til]=i;
    }
    printf("%lld",f[n]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值