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.
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 one line with an integer.
#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; }
原文:http://www.cnblogs.com/zero-begin/p/4649572.html