标签:print 优化 def hint limit mil nbsp turn 16px
似乎逐渐掌握了斜率优化的规律,,,
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 }
标签:print 优化 def hint limit mil nbsp turn 16px
原文地址:https://www.cnblogs.com/InWILL/p/9595401.html