FZU2013 short problem

Problem Description

The description of this problem is very short. Now give you a string(length N), and ask you the max sum of the substring which the length can't small than M.

 Input

The first line is one integer T(T≤20) indicates the number of the test cases. Then for every case, the first line is two integer N(1≤N≤1000000) and M(1≤M≤N).

Then one line contains N integer indicate the number. All the number is between -10000 and 10000.

 Output

Output one line with an integer.

 Sample Input

2 5 1 1 -2 -2 -2 1 5 2 1 -2 -2 -2 1

 Sample Output

1 -1

 Source

FOJ有奖月赛-2011年03月
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<map>

#include<string>

#include<cmath>

using namespace std;

int a[1000010];



int main()

{

    int n,m;

    int T;

    scanf("%d", &T);

    while(T--){

        scanf("%d%d", &n, &m);

        for(int i = 1; i <= n ; i++)

            scanf("%d", &a[i]);

        for(int i = 1; i <= n ;i++){

            a[i] += a[i-1];

        }

        int t = 0;

        int ans = -1;

        for(int i = m; i <= n ; i++){

            ans = max(ans , a[i] - t);

            t = min(t, a[i-m]);

        }

        printf("%d\n", ans);

    }

    return 0;

}

  

 

你可能感兴趣的:(sh)