斜率优化。
事实上是选一个大于某个数的最小斜率。维护下凸壳。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 50050 using namespace std; long long n,c,s[maxn],dp[maxn],q[maxn],l=1,r=0; double slop(long long j,long long k) { return (dp[k]-dp[j]+(s[k]+c)*(s[k]+c)-(s[j]+c)*(s[j]+c))/(2.0*(s[k]-s[j])); } void dps() { q[++r]=0; for (long long i=1;i<=n;i++) { while ((l<r) && (slop(q[l],q[l+1])<s[i])) l++; long long t=q[l]; dp[i]=dp[t]+(s[i]-s[t]-c)*(s[i]-s[t]-c); while ((l<r) && (slop(q[r-1],i)<slop(q[r-1],q[r]))) r--; q[++r]=i; } } int main() { scanf("%lld%lld",&n,&c);c++; for (long long i=1;i<=n;i++) scanf("%lld",&s[i]); for (long long i=1;i<=n;i++) s[i]+=s[i-1]; for (long long i=1;i<=n;i++) s[i]+=i; dps(); printf("%lld\n",dp[n]); return 0; }