zoukankan      html  css  js  c++  java
  • BZOJ2442 Usaco2011 Open修剪草坪(动态规划+单调队列)

      显然可以dp。显然可以单调队列优化一下。

    #include<iostream> 
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int read()
    {
        int x=0,f=1;char c=getchar();
        while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
        while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
        return x*f;
    }
    #define N 100010
    int n,m,q[N];
    long long a[N],f[N];
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("bzoj2442.in","r",stdin);
        freopen("bzoj2442.out","w",stdout);
        const char LL[]="%I64d
    ";
    #else
        const char LL[]="%lld
    ";
    #endif
        n=read(),m=read();
        for (int i=1;i<=n;i++) a[i]=a[i-1]+read();
        int head=1,tail=1;
        for (int i=1;i<=n+1;i++)
        {
            while (q[head]<i-m-1) head++;
            f[i]=f[q[head]]+a[i-1]-a[q[head]];
            while (head<=tail&&f[q[tail]]-a[q[tail]]<=f[i]-a[i]) tail--;
            q[++tail]=i;
        }
        cout<<f[n+1];
        return 0;
    }
  • 相关阅读:
    Python day thirteen
    Python day twelve
    Python day eleven
    Python day ten
    Python day nine
    Python day eight
    Python day seven
    Python day six
    Python day five
    Python day four
  • 原文地址:https://www.cnblogs.com/Gloid/p/9573609.html
Copyright © 2011-2022 走看看