1010: [HNOI2008]玩具装箱toy

第一次写斜率优化。。。首先一定要记住最好不用除法,因为会有精度误差=,=。。。。

 4 #include<iostream>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<cstring>
 8 #include<cstdio>
 9 #include<algorithm>
10 #include<string>
11 #include<map>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #define inf 1000000000
16 #define maxn 50000+10
17 #define maxm 10000+5
18 #define eps 1e-10
19 #define ll long long
20 #define for0(i,n) for(int i=0;i<=(n);i++)
21 #define for1(i,n) for(int i=1;i<=(n);i++)
22 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
23 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
24 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
25 using namespace std;
26 ll c[maxn],f[maxn],q[maxn];int tmp;
27 int read(){
28     int x=0,f=1;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
30     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
31     return x*f;
32 }
33 ll G(int a,int b) {
34  return (f[a]+(c[a]+tmp)*(c[a]+tmp)-f[b]-(c[b]+tmp)*(c[b]+tmp));
35 }
36 ll S(int a,int b) {
37  return (2*(c[a]-c[b]));
38 }
39 int main(){
40     //freopen("input.txt","r",stdin);
41     //freopen("output.txt","w",stdout);
42     int n=read(),l=read();tmp=l+1;
43     for1(i,n)c[i]=read()+c[i-1];
44     for1(i,n)c[i]+=i;
45     int head=0,tail=0;
46     q[tail++]=0;
47     for1(i,n){
48         while(head<tail-1&&G(q[head+1],q[head])<=c[i]*S(q[head+1],q[head]))++head;
49         int x=q[head];
50         f[i]=f[x]+(c[i]-c[x]-tmp)*(c[i]-c[x]-tmp);
51         q[tail++]=i;
52         for(int j=tail-2;j>head;--j){
53              int z=q[j+1],y=q[j],x=q[j-1];
54             if(!(G(y,x)*S(z,y)<G(z,y)*S(y,x)))q[j]=q[--tail];
55              else break;
56         }
57     }
58     cout<<f[n];
59     return 0;
60 }
View Code

 

请看这份题解:http://www.cnblogs.com/perseawe/archive/2012/05/12/BZ1010.html
posted @ 2016-02-11 10:03  HTWX  阅读(108)  评论(0编辑  收藏  举报