luogu P1714 切蛋糕 单调队列

单调队列傻题.

考虑以 $i$ 结尾的答案 : $max(sumv_{i}-sumv_{j}),j \in [i-m,i-1]$

($sumv_{i}$ 为前缀和)

稍微搞一搞,发现 $sumv_{i}$ 这个是固定的.

我们只需维护 $min(sumv_{j})$ 即可.

单调队列优化一下,每次取队首即可.
 

Code:

#include<cstdio>
#include<deque>
#include<algorithm>
using namespace std;
const int maxn = 500000+3;
const int inf = -100000000;
long long sumv[maxn];
deque<int>Q;
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,ans = inf;
    scanf("%d%d",&n,&m);
    for(int i =1;i <= n;++i){
        int w;
        scanf("%d",&w);
        sumv[i] = sumv[i-1]+w;
    }
    for(int i =1; i<=n ;++i){
        int cur = i- m;
        while(!Q.empty() && Q.front()<cur)Q.pop_front();
        while(!Q.empty() && sumv[Q.back()] >= sumv[i])Q.pop_back();
        Q.push_back(i);
        int h = (int)(sumv[i] - sumv[Q.front()]);
        ans = max(ans,h);
    }
    printf("%d",ans);
    return 0;
}

  

posted @ 2019-06-17 18:57  EM-LGH  阅读(134)  评论(0编辑  收藏  举报