首页 > 其他 > 详细

单调队列 I

时间:2015-12-01 21:11:21      阅读:234      评论:0      收藏:0      [点我收藏+]

2009国家集训队徐持衡的论文《浅谈几类背包问题》里提到的一个经典问题:

长度限制最大连续和问题:

  给出长度为 n 的序列 X i ,求这个序列中长度不超过 Lmax 的最大连续和。

 

Implementation

 

#include <bits/stdc++.h>
using namespace std;
const int N(1e5+5);
typedef pair<int,int> P;
P que[N];

int main(){
    int n, l, ans=0, head=0, tail=0;
    scanf("%d%d", &n, &l);
    int s=0;
    que.push({0, s});
    for(int i=1, a; i<=n; i++){
        scanf("%d", &a);
        s+=a;
        for(; head!=tail && que[head].first<=i-l; head++);
        for(; head!=tail && que[tail-1].second>=s; tail--);
        que[tail++]={i, s};
        ans=max(ans, s-que[head].second);
    }
    printf("%d\n", ans);
}

 

单调队列 I

原文:http://www.cnblogs.com/Patt/p/5011087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!