[BZOJ3156] 防御准备

f[i] 表示第 i 个检查站的位置建防御塔的最小花费

那么

f[i]=Min(f[j]+k=j+1i1(ik))

考虑用斜率优化

j<k k j更优,
那么可以得到

f[k]+(ik)(ik1)2f[j]+(ij)(ij1)2

化简得

2(f[k]f[j])+k2j2+kj2(kj)i

slope(k,j)=2(f[k]f[j])+k2j2+kj2(kj)

于是可以得到,弹队首的条件:满足 slope(l,l+1)<=i
弹队尾的条件:满足 slope(r,r1)>slope(i,r)
时间复杂度 O(N)

#include <cstdio>

using namespace std;

template <class T>T Max (const T &a, const T &b) {return a > b ? a : b;}
template <class T>T Min (const T &a, const T &b) {return a < b ? a : b;}

typedef long long LL;
typedef double dl;

const int SN = 1000000 + 100;

LL a[SN], n, f[SN];
int que[SN], head, tail;

void Read(LL &x) {
    LL in = 0, f = 1;char  ch = getchar();
    while(ch<'0' || ch>'9') {if(ch=='-') f = -1;ch = getchar();}
    while(ch>='0' && ch<='9') {in = in*10+ch-'0'; ch = getchar();}
    x = in*f;
}

LL get_up(LL i,LL j) { //i,j都要开long long 
    return 2*(f[i]-f[j])+i*i-j*j+i-j;
}

dl get_slope(LL i,LL j) {
    return (double)((get_up(i,j))/(double)(i-j));
}

int main() {
    Read(n);

    for(int i = 1; i <= n; i++) Read(a[i]);

    for(int i = 1; i <= n; i++) {
        while(head < tail && get_slope(que[head+1],que[head])<= 2*i) head++;
        f[i] = f[que[head]] + (i-que[head])*(i-que[head]-1)/2 + a[i];
           while(head < tail && get_slope(i, que[tail]) <= get_slope(que[tail], que[tail-1]))
           tail--;
           que[++tail] = i;
    }

    printf("%lld\n",f[n]);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值