码迷,mamicode.com
首页 > 其他好文 > 详细

单调队列 I

时间:2015-12-01 21:14:34      阅读:205      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!