loj10188. 「一本通 5.6 练习 1」玩具装箱

思路:这道题的原始dp方程貌似都给在题里了。。。纯粹按模板敲个斜率优化就A过去了。。

#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 50010;
inline long long qread(){
    register int ch = getchar();
    register long long x = 0;
    while(ch < '0' || ch > '9')    ch = getchar();
    while(ch >= '0' && ch <= '9')    x = (x << 3) + (x << 1) + ch - 48, ch = getchar();
    return x;
}
long long C[maxn];
long long sum[maxn];
long long f[maxn];
int n;
long long L;
long long qu[maxn << 5];
int l = 1, r = 1;
inline long long fy(int x){
    return f[x] + (x + sum[x]) * (x + sum[x]);
}
inline long long fx(int x){
    return x + sum[x];
}
int main(void){
    scanf("%d", &n);
    L = qread();
    for(int i = 1; i <= n; ++i){
        C[i] = qread();
        sum[i] = sum[i - 1] + C[i];
    }
    for(int i = 1; i <= n; ++i){
        while(l < r && (fy(qu[l + 1]) - fy(qu[l])) < ((i + sum[i] - 1 - L) << 1) * (fx(qu[l + 1]) - fx(qu[l])))    ++l;
        f[i] = f[qu[l]] + (i - qu[l] - 1 + sum[i] - sum[qu[l]] - L) * (i - qu[l] - 1 + sum[i] - sum[qu[l]] - L);
        while(l < r && (fy(qu[r]) - fy(qu[r - 1])) * (fx(i) - fx(qu[r])) > (fy(i) - fy(qu[r])) * (fx(qu[r]) - fx(qu[r - 1])))    --r;
        qu[++r] = i; 
    }
    printf("%lld\n", f[n]);
}

 

转载于:https://www.cnblogs.com/junk-yao-blog/p/9531264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值