Problem A: 连续子串和

Description

小Y前段时间刚刚做完连续子串和问题,相信大家对连续子串和也都不陌生,现在小Y又碰到了一个新的难题,给定N个整数,求至少K个数连续的最大和为多少?

Input

若干组测试数据,每组数据第一行给出两个数N(1<=N<=10^6),K(1<=K<=N)。接下来一行N个数字,分别是a1,a2...aN,对于每个数满足-1000<=ai<=1000。

Output

输出长度大于等于K的连续整数和值中最大的一个。

Sample Input

5 3-2 3 6 0 1

Sample Output

10

HINT


#include<stdio.h>
#include<string.h>
#include<cmath>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

const int Max = 10005;
const int Inf = 1<<30;

int main()
{
//freopen("Input.txt","r",stdin);
    int i,j,k,n,m;
    int a,b;
    while(~scanf("%d%d",&n,&m))
    {
        int maxx = -9999 ;
        int sum = 0;
        b = 0;
        vector<int> V;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%d",&a);
// printf("mmax ---- %d\n",maxx);
            if(V.size() < m)
            {
                sum += a;
                V.push_back(a);
                maxx = sum;
                continue;

            
            }
            V.push_back(a);
            sum += a;
            maxx = max(maxx , sum);
            k = V.size() - m;
            b += V[k-1];
            if(b <= 0)
            {
                sum -= b;
                V.erase(V.begin(),V.begin()+k-1);
                b = 0;
            }
        maxx = max(maxx , sum);
        }
        printf("%d\n",maxx);
    }
}


posted @ 2013-04-12 21:59  bo_jwolf  阅读(160)  评论(0)    收藏  举报