luogu3628 特别行动队 (斜率优化dp)

推出来式子以后斜率优化水过去就完事了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 #include<cmath>
 7 #define inf 0x3f3f3f3f
 8 #define LL long long int
 9 using namespace std;
10 const int maxn=1000010;
11 
12 inline LL rd(){
13     LL x=0;char c=getchar();int neg=1;
14     while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
15     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
16     return x*neg;
17 }
18 
19 int N;
20 LL f[maxn],x[maxn],s[maxn],A,B,C;
21 int q[maxn],head,tail;
22 
23 inline LL pw2(LL x){return x*x;}
24 
25 inline bool judge1(int j1,int j2,int i){
26     return (f[j1]+A*pw2(s[j1])-f[j2]-A*pw2(s[j2]))>2*A*s[i]*(s[j1]-s[j2]);
27 }
28 inline bool judge2(int j1,int j2,int j3,int i){
29     return (f[j1]+A*pw2(s[j1])-f[j2]-A*pw2(s[j2]))*(s[j2]-s[j3])<
30            (f[j2]+A*pw2(s[j2])-f[j3]-A*pw2(s[j3]))*(s[j1]-s[j2]);
31 }
32 
33 int main(){
34     //freopen("3628.in","r",stdin);
35     int i,j,k;
36     N=rd();A=rd();B=rd();C=rd();
37     for(i=1;i<=N;i++) x[i]=rd(),s[i]=s[i-1]+x[i];
38     LL ans=0;
39     tail=head=1;q[1]=0;
40     for(i=1;i<=N;i++){
41         while(head<tail&&!judge1(q[head],q[head+1],i)) head++;
42         f[i]=f[q[head]]+A*pw2(s[i]-s[q[head]])+C;
43         while(head<tail&&judge2(q[tail-1],q[tail],i,i)) tail--;
44         q[++tail]=i;
45     }printf("%lld\n",f[N]+B*s[N]);
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/Ressed/p/9457513.html