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

32 lines
1.0 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
const int N = 100010;
typedef long long LL;
int q[N];
LL s[N], f[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> s[i], s[i] += s[i - 1];
// 由于滑动窗口在i的左边,需要讨论前缀和的s[l-1],第1个没有前序,不符合整体的代码逻辑,添加了一个哨兵
int hh = 0, tt = 0;
for (int i = 1; i <= n; i++) {
//  1、年龄大于m的老家伙们,不管是不是实力够强大,一概去死
while (hh <= tt && i - q[hh] > m) hh++;
// 因为滑动窗口在i 左侧,先使用再加入
f[i] = max(f[i - 1], f[max(0, q[hh] - 1)] + s[i] - s[q[hh]]);
//  2、不如我年轻,并且,不如我有实力的老家伙们去死
while (hh <= tt && f[i - 1] - s[i] >= f[max(0, q[tt] - 1)] - s[q[tt]]) tt--;
// 3、i入队列
q[++tt] = i;
}
// 输出结果
cout << f[n] << endl;
return 0;
}