码迷,mamicode.com
首页 > 其他好文 > 详细

luogu1010/bzoj3195 玩具装箱(斜率优化dp)

时间:2018-08-10 23:08:16      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:amp   max   turn   fine   getc   for   color   def   bool   

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

 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 }

 

luogu1010/bzoj3195 玩具装箱(斜率优化dp)

标签:amp   max   turn   fine   getc   for   color   def   bool   

原文地址:https://www.cnblogs.com/Ressed/p/9457494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!