2018 UESTC Training for Dynamic Programming | 愿你有一天能和你重要的人重逢

这题就是这题稍稍变了一丁点(几乎没变)

解析可参考上一篇博客

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
typedef long double db;

#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define eps double(1e-6)
#define pi acos(-1.0)
#define lson  root << 1
#define rson  root << 1 | 1
#define pb push_back


ll dp[100005<<1];

ll sum[100005<<1];

ll q[100005<<1];

ll head,tail,n,m;

ll len;

ll getDP(int i,int j)
{
    return dp[j]+(len+sum[i]-sum[j]-m)*(len+sum[i]-sum[j]-m);
}

ll getUP(int j,int k)
{
    return (dp[j]+sum[j]*sum[j]+2ll*(m-len)*sum[j])-(dp[k]+sum[k]*sum[k]+2ll*(m-len)*sum[k]);
}

ll getDOWN(int j,int  k)
{
    return 2ll*(sum[j]-sum[k]);
}


ll a[100005<<1];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>len>>m;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    sum[0]=dp[0]=0;
    for(int i=1; i<=n; i++)
        sum[i]=sum[i-1]+a[i];
    for(int i=1; i<=n; i++)
        sum[i]+=i;
    head=tail=0;
    q[tail++]=0;
    for(int i=1; i<=n; i++)
    {
        while(head+1<tail && getUP(q[head+1],q[head])<=(sum[i])*getDOWN(q[head+1],q[head]))
            head++;
        dp[i]=getDP(i,q[head]);
        while(head+1<tail && getUP(i,q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=getUP(q[tail-1],q[tail-2])*getDOWN(i,q[tail-1]))
            tail--;
        q[tail++]=i;
    }
    cout<<dp[n]<<endl;
}