luogu3195 [HNOI2008]玩具装箱TOY

懒得写

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long longlivetheelder;
int n, l, ll, rr, que[50005];
longlivetheelder w[50005], dp[50005];
struct Point{
    longlivetheelder x, y;
}pt[50005];
longlivetheelder f(longlivetheelder x){
    return x*x;
}
double getK(const Point &u, const Point &v){
    return (u.y-v.y)/(u.x-v.x);
}
int main(){
    cin>>n>>l;
    l++;
    for(int i=1; i<=n; i++){
        scanf("%lld", &w[i]);
        w[i] += w[i-1];
        while(ll<rr && getK(pt[que[ll]], pt[que[ll+1]])<(w[i]+i)*2)	ll++;
        dp[i] = dp[que[ll]] + f(i-que[ll]+w[i]-w[que[ll]]-l);
        pt[i] = (Point){w[i]+i, dp[i]+2*l*(w[i]+i)+f(w[i]+i)};
        while(ll<rr && getK(pt[que[rr]], pt[que[rr-1]])>=getK(pt[que[rr]],pt[i]))
            rr--;
        que[++rr] = i;
    }
    cout<<dp[n]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8933704.html