Solution -「NOIOLS 3」水壶

cirnovsky /

作为提高组的题还是水了点。

题目让我们求一个长度限制为 k+1k+1 的最大子段和,又因为 AiA_{i} 非负,所以直接前缀和求 max(sumi+ksumi1)\max(sum_{i+k}-sum_{i-1})

(开始没看 AiA_{i} 的范围还锤了半天单调队列……)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1e6 + 5;
int n, k, a[N];
long long sum[N], dp[N];

signed main() {
	freopen("kettle.in", "r", stdin);
	freopen("kettle.out", "w", stdout);
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
	long long ans = 0;
	for (int i = 1; i <= n - k; ++i) ans = max(ans, sum[i + k] - sum[i - 1]);
	return printf("%lld\n", ans), 0;
}