题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1911
斜率优化水题。别忘了斜率是递减的。
#include<iostream> #include<cstdio> #include<cstring> #define ll long long #define db double using namespace std; const int N=1e6+5; int n,h,t,q[N]; ll a,b,c,s[N]; db dp[N]; db x(int k){return s[k];} db y(int k){return a*s[k]*s[k]-b*s[k]+dp[k];} double slope(int u,int v){return (y(v)-y(u))/(x(v)-x(u));} int main() { scanf("%d%lld%lld%lld",&n,&a,&b,&c);ll tp; for(int i=1;i<=n;i++)scanf("%lld",&tp),s[i]=s[i-1]+tp; h=1;t=1; for(int i=1;i<=n;i++) { while(h<t&&slope(q[h],q[h+1])>=(double)2*a*s[i])h++; dp[i]=y(q[h])+a*s[i]*s[i]+b*s[i]+c-2*a*s[i]*x(q[h]); while(h<t&&slope(q[t-1],q[t])<=slope(q[t-1],i))t--; q[++t]=i; } printf("%lld",(ll)dp[n]); return 0; }