题解 | #数学考试#
数学考试
https://ac.nowcoder.com/acm/problem/15553
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[2000005]; int main(){ int t; cin >> t; while(t--) { int n, k; cin >> n >> k; for(int i=1; i<=n; i++) cin >> a[i],a[i] += a[i-1]; ll m = -1e18, ans = -1e18; for(int i = k; i + k <= n; i ++) { m = max(m, a[i] - a[i-k]); ans = max(ans, m + a[i+k] - a[i]); } cout << ans << endl; } return 0; }
对于本题可能存在如下疑问:
如:2 3 4 1 2 3 4 令a为2 3 4, b为1 2 3, c为3 4 1, d为2 3 4
此时c + d > a + b,但事实上此时的最大值应为a + d而非c + d,因此不存在错误