luogu3195/bzoj1010 玩具装箱(斜率优化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=50050;
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 co[maxn],sm[maxn],f[maxn],L;
21 int q[maxn],h,t;
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]+pw2(j1+sm[j1])-f[j2]-pw2(j2+sm[j2])<(2*i+2*sm[i]-2*L-2)*(j1+sm[j1]-j2-sm[j2]);
27 }
28 inline bool judge2(int j1,int j2,int j3){
29     return (f[j1]+pw2(j1+sm[j1])-f[j2]-pw2(j2+sm[j2]))*(j2+sm[j2]-j3-sm[j3])<
30            (f[j2]+pw2(j2+sm[j2])-f[j3]-pw2(j3+sm[j3]))*(j1+sm[j1]-j2-sm[j2]);
31 }
32 
33 int main(){
34     int i,j,k;
35     N=rd();L=rd();
36     for(i=1;i<=N;i++)co[i]=rd(),sm[i]=sm[i-1]+co[i];
37     h=t=1;q[1]=0;
38     for(i=1;i<=N;i++){
39         while(h<t&&!judge1(q[h],q[h+1],i)) h++;
40         f[i]=f[q[h]]+pw2(i-q[h]-1+sm[i]-sm[q[h]]-L);
41         while(h<t&&!judge2(q[t-1],q[t],i)) t--;
42         q[++t]=i;
43     }printf("%lld\n",f[N]);
44     
45     return 0;
46 }

 

posted @ 2018-08-10 21:25  Ressed  阅读(166)  评论(0编辑  收藏  举报