首页 > Windows开发 > 详细

1911: [Apio2010]特别行动队

时间:2018-09-06 01:42:23      阅读:170      评论:0      收藏:0      [点我收藏+]
Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 5706  Solved: 2876
[Submit][Status][Discuss]

Description

技术分享图片

Input

技术分享图片

Output

技术分享图片

Sample Input

4
-1 10 -20
2 2 3 4

Sample Output

9

HINT

技术分享图片

 

似乎逐渐掌握了斜率优化的规律,,,

 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 }

 

1911: [Apio2010]特别行动队

原文:https://www.cnblogs.com/InWILL/p/9595401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!