# 洛谷P3195||bzoj1010 [HNOI2008]玩具装箱TOY

bzoj1010

（斜率优化dp）参考(复读)https://www.cnblogs.com/orzzz/p/7885971.html

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<vector>
5 using namespace std;
6 #define fi first
7 #define se second
8 #define mp make_pair
9 #define pb push_back
10 typedef long long ll;
11 typedef unsigned long long ull;
12 struct P
13 {
14     ll x,y,n;
15 };
16 P operator-(const P &a,const P &b)
17 {
18     return (P){a.x-b.x,a.y-b.y,0};
19 }
20 ll cross(const P &a,const P &b)
21 {
22     return a.x*b.y-b.x*a.y;
23 }
24 inline ll sqr(ll x){return x*x;}
25 P tmp[100011];int l,r;
26 int n;ll L;
27 ll s[100011],ans[100011];
28 int main()
29 {
30     int i;ll t1;P tn;
31     scanf("%d%lld",&n,&L);
32     for(i=1;i<=n;++i)
33     {
34         scanf("%lld",s+i);
35         s[i]+=s[i-1];
36     }
37     l=1;r=0;
38     tmp[++r]=(P){0,0,0};
39     for(i=1;i<=n;++i)
40     {
41         t1=2*(i+s[i]);
42         while(l<r && tmp[l+1].y-tmp[l].y <= t1*(tmp[l+1].x-tmp[l].x))    ++l;
43         if(l>r)    l=r;
44         ans[i]=ans[tmp[l].n]+sqr(i-tmp[l].n-1+s[i]-s[tmp[l].n]-L);
45         tn=(P){i+s[i],ans[i]+(i+s[i])*(i+s[i]+2*L+2),i};
46         while(r>=2 && cross(tn-tmp[r-1],tmp[r]-tmp[r-1])>=0)    --r;
47         tmp[++r]=tn;
48     }
49     printf("%lld\n",ans[n]);
50     return 0;
51 }
View Code

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<vector>
5 using namespace std;
6 #define fi first
7 #define se second
8 #define mp make_pair
9 #define pb push_back
10 typedef long long ll;
11 typedef unsigned long long ull;
12 struct P
13 {
14     ll x,y,n;
15 };
16 P operator-(const P &a,const P &b)
17 {
18     return (P){a.x-b.x,a.y-b.y,0};
19 }
20 ll cross(const P &a,const P &b)
21 {
22     return a.x*b.y-b.x*a.y;
23 }
24 inline ll sqr(ll x){return x*x;}
25 P tmp[100011];int l,r;
26 int n;ll L;
27 ll s[100011],ans[100011];
28 int main()
29 {
30     int i;ll t1;P tn;
31     scanf("%d%lld",&n,&L);
32     for(i=1;i<=n;++i)
33     {
34         scanf("%lld",s+i);
35         s[i]+=s[i-1];
36     }
37     l=1;r=0;
38     tmp[++r]=(P){L+1,sqr(L+1),0};
39     for(i=1;i<=n;++i)
40     {
41         t1=2*(i+s[i]);
42         while(l<r && tmp[l+1].y-tmp[l].y <= t1*(tmp[l+1].x-tmp[l].x))    ++l;
43         if(l>r)    l=r;
44         ans[i]=ans[tmp[l].n]+sqr(i-tmp[l].n-1+s[i]-s[tmp[l].n]-L);
45         tn=(P){i+s[i]+L+1,ans[i]+sqr(i+s[i]+L+1),i};
46         while(r>=2 && cross(tn-tmp[r-1],tmp[r]-tmp[r-1])>=0)    --r;
47         tmp[++r]=tn;
48     }
49     printf("%lld\n",ans[n]);
50     return 0;
51 }
View Code

https://www.cnblogs.com/MashiroSky/p/6009685.html

https://blog.csdn.net/lxc779760807/article/details/51366552

https://codeforces.com/blog/entry/63823

https://www.cnblogs.com/flashhu/p/9480669.html

posted @ 2019-03-08 15:06  hehe_54321  阅读(240)  评论(0编辑  收藏  举报