Submit: 5706 Solved: 2876
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
似乎逐渐掌握了斜率优化的规律,,,
f[i]=max{f[j]+a*(sum[i]-sum[j])2+b*(sum[i]-sum[j]+c}
斜率优化,设k<j<i,且j比k更优
[f[i]-f[k]+a(s[j]^{2}-s[k]^{2})+b(s[k]-s[j])>2a imes s[i] imes (s[j]-s[k])]
化为:(注意a为负数!!!要变号)
[frac{f[i]-f[k]+a(s[j]^{2}-s[k]^{2})+b(s[k]-s[j])}{2a(s[j]-s[k])}<s[i]]
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 #define LL long long 6 #define sqr(x) ((x)*(x)) 7 const int MAXN=1000005; 8 9 int n,a,b,c; 10 int l,r,q[MAXN]; 11 LL f[MAXN],s[MAXN]; 12 13 LL cal(LL x) 14 { 15 return a*sqr(x)+b*x+c; 16 } 17 18 double slope(int k,int j) 19 { 20 return (double)(f[j]-f[k]+a*(sqr(s[j])-sqr(s[k]))+b*(s[k]-s[j]))/(2*a*(s[j]-s[k])); 21 } 22 23 int main() 24 { 25 scanf("%d",&n); 26 scanf("%d%d%d",&a,&b,&c); 27 for(int i=1;i<=n;i++) 28 { 29 int x; 30 scanf("%d",&x); 31 s[i]=s[i-1]+x; 32 } 33 for(int i=1;i<=n;i++) 34 { 35 while(l<r&&slope(q[l],q[l+1])<s[i]) l++; 36 int t=q[l]; 37 f[i]=f[t]+cal(s[i]-s[t]); 38 while(l<r&&slope(q[r],i)<slope(q[r-1],q[r])) r--; 39 q[++r]=i; 40 } 41 printf("%lld",f[n]); 42 return 0; 43 }