Files
python/TangDou/AcWing/MonotonicQueue/135_BruteForce.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

25 lines
772 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
// Brute-force 暴力
const int N = 300010;
const int INF = 0x3f3f3f3f;
int s[N];
int n, m;
// TLE掉3个点,通过了 11/14个数据
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> s[i], s[i] += s[i - 1];
int res = -INF;
// 遍历每一个终点
for (int i = 1; i <= n; i++)
// 从终点向前,找出所有区间在m之内的数字,通过前缀和计算出区间的累加和,保留最大值
// 在刚刚出发不久,长度不够m的时候,即i<m,则i-m会出现负数
for (int j = i; j > max(0, i - m); j--)
res = max(res, s[i] - s[j - 1]); // j>0 && j> i-m --> j> max(0,i-m)
printf("%d\n", res);
return 0;
}