求条!
查看原帖
求条!
766675
da_ke楼主2025/2/3 14:11
#include <bits/stdc++.h>

#define i64 long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define fdn(i,r,l) for(int i=(r);i>=(l);i--)
#define pii pair<int,int>
using namespace std;

typedef long long ll;
typedef double db;
typedef __int128 i128;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937_64 rnd64(std::chrono::steady_clock::now().time_since_epoch().count());

const int fyy=0;

ll pw(ll x){return x*x;}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int N;ll L;
    cin>>N>>L;
    L++; // 为了方便
    vector<ll> S(N+1,0);
    rep(i,1,N) cin>>S[i],S[i]+=S[i-1];
    rep(i,1,N) S[i]+=i;
    vector<ll> dp(N+1,0),q(N+1,0);
    int l=0,r=0;
    auto myabs=[](ll k)->ll {return (k<0)?-k:k;};
    rep(i,1,N)
    {
        auto X= [dp,S,L](int k) -> ll {return S[k]+L;};
        auto Y= [dp,S,L](int k) -> ll {return dp[k]+pw(S[k]+L);};
        while(l<r&&
            myabs(Y(q[l+1])-Y(q[l]))
            <=2*S[i]*myabs(X(q[l+1])-X(q[l]))) l++;
        int j=q[l]; // 最优决策点。
        dp[i]=dp[j]+pw(S[i]-S[j]-L);
        while(l<r&&
            myabs(
                (Y(q[r-1])-Y(q[r]))*
                (X(q[r])-X(i))
                )
            >=myabs(
                (X(q[r-1])-X(q[r]))*
                (Y(q[r])-Y(i)))
                ) r--;
        q[++r]=i;
    }
    cout<<dp[N]<<endl;
}

// 路虽远行则将至,事虽难做则必成。

用手机写的,不知错在哪里!

玄关

2025/2/3 14:11
加载中...