AcWing 135. 最大子序和
原题链接
简单
作者:
愿无忧
,
2021-02-16 22:04:29
,
所有人可见
,
阅读 341
C++ 代码
#include <iostream>
#include <queue>
#include<algorithm>
using namespace std;
const int N = 300010;
int n, m;
int s[N];
deque<int>q;
int main(){
cin >> n >> m;
for (int i = 1; i<=n; i++){
cin >> s[i];
s[i] += s[i - 1];
}
int res = s[1];
q.push_back(0);
for (int i = 1; i<=n; i++){
if (q.size() && q.front()<i - m)
q.pop_front();
res = max(res, s[i] - s[q.front()]);
while (q.size() && s[q.back()] >= s[i])
q.pop_back();
q.push_back(i);
}
cout << res << endl;
system("pause");
return 0;
}