题意中文。
做法:也是斜率优化的基础题。就是化简的时候注意不要写错了。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 201000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
const int maxn = 50000+10;
int q[maxn];
ll sum[maxn],dp[maxn],l;
ll h[maxn];
ll gety(int a)
{
return dp[a]+h[a]*h[a]+2LL*l*h[a];
}
ll getx(int a)
{
return 2LL*h[a];
}
ll suby(int a, int b)
{
return gety(a)-gety(b);
}
ll subx(int a, int b)
{
return getx(a)-getx(b);
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o.txt","w",stdout);
#endif
int n;
sum[0] = 0;
while(~scanf("%d%lld",&n,&l))
{
for(int i = 1; i <= n; i++)
{
ll t;
scanf("%lld",&t);
sum[i] = sum[i-1]+t;
h[i] = sum[i]+(ll)i;
}
int head = 0,rear = 1;
q[0] = dp[0] = 0;
for(int i = 1; i <= n; i++)
{
while(rear-head > 1 && suby(q[head+1],q[head]) <= (h[i]-1LL)*subx(q[head+1],q[head]))
head++;
dp[i] = gety(q[head])-(h[i]-1LL)*getx(q[head])+(l-h[i]+1LL)*(l-h[i]+1LL);
while(rear-head > 1 && suby(i,q[rear-1])*subx(q[rear-1],q[rear-2]) <= suby(q[rear-1],q[rear-2])*subx(i,q[rear-1]))
rear--;
q[rear++] = i;
}
printf("%lld\n",dp[n]);
}
return 0;
}