Problem A: 连续子串和

79 篇文章 0 订阅
18 篇文章 0 订阅

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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值