bzoj 3437: 小P的牧场

23333,随便推

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define lowbit(x) x&(-x)
 4 #define inf 0x3f3f3f3f
 5 #define eps 1e-5
 6 #define N 1000005
 7 using namespace std;
 8 inline LL ra()
 9 {
10     LL x=0,f=1; char ch=getchar();
11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
13     return x*f;
14 }
15 LL B[N],sum[N],a[N],b[N],f[N];
16 int n,q[N];
17 double slope(int j, int k)
18 {
19     return (f[j]-f[k]-sum[k]+sum[j])/(double)(B[j]-B[k]);
20 }
21 int main()
22 {
23     n=ra();
24     for (int i=1; i<=n; i++) a[i]=ra();
25     for (int i=1; i<=n; i++) b[i]=ra();
26     for (int i=1; i<=n; i++) B[i]=B[i-1]+b[i];
27     for (int i=1; i<=n; i++) sum[i]=sum[i-1]+(LL)b[i]*i;
28     int l=0,r=0;
29     for (int i=1; i<=n; i++)
30     {
31         while (l<r && slope(q[l+1],q[l])<i) l++;
32         int t=q[l];
33         f[i]=f[t]+a[i]+(LL)i*(B[i]-B[t])-sum[i]+sum[t];
34         while (l<r && slope(i,q[r])<slope(q[r],q[r-1])) r--;
35         q[++r]=i;
36     }
37     cout<<f[n];
38     return 0;
39 }

 

posted @ 2017-03-13 20:52  ws_ccd  阅读(126)  评论(0编辑  收藏  举报