zoukankan      html  css  js  c++  java
  • bzoj3437 小P的牧场

    题目链接

    斜率优化n^2的DP

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int yp()
     5 {
     6     int x=0,f=0;char ch=getchar();
     7     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
     8     while(ch>='0'&&ch<='9')x*=10,x+=ch-'0',ch=getchar();
     9     return !f?x:-x;
    10 }
    11 int n,l=1,r,q[1000005];
    12 long long a[1000005],b[1000005],tot,ans,h[1000005],sum[1000005];
    13 double js(int a,int b)
    14 {
    15     return (1.*(h[b]-h[a]))/(1.*(a-b));
    16 }
    17 int main()
    18 {
    19     n=yp();
    20     for(int i=1;i<=n;i++)a[i]=yp();
    21     for(int i=1;i<=n;i++)b[i]=yp(),sum[i]=sum[i-1]+b[i];
    22     for(int i=1;i<n;i++)tot+=b[i]*(n-i);
    23     tot+=a[n],q[++r]=n;
    24     for(int i=n-1;i;i--)
    25     {
    26         while(l<r&&js(q[l],q[l+1])>sum[i])l++;
    27         int j=q[l];
    28         h[i]=h[j]+sum[i]*(j-i)-a[i];
    29         ans=max(ans,h[i]);
    30         while(l<r&&js(q[r],i)>js(q[r-1],q[r]))r--;
    31         q[++r]=i;
    32     }
    33     printf("%lld",tot-ans);
    34     return 0;
    35 }
  • 相关阅读:
    切换node版本
    vue 移动端组件
    vue 单元测试
    git
    每日日报
    每日日报
    每日日报
    01人月神话阅读笔记之三
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/HugeGun/p/5151442.html
Copyright © 2011-2022 走看看