周赛单调队列模板题,但因为一点小细节一直wa...
忘写了这一句,因为是对它所有的求,所以不能忽略0的影响....0_0
牢记!牢记!
#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 1e5 + 6;
int a[N], b[N];
deque<int> q;
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
int ans = -0x3f3f3f3f;
for(int i = 1; i <= n; i++) cin >> a[i], b[i] = b[i - 1] + a[i];
q.push_back(0);
for(int i = 1; i <= n; i++){
while(!q.empty() && b[q.back()] >= b[i]) q.pop_back();
q.push_back(i);
while(!q.empty() && q.back() - q.front() > m) q.pop_front();
if((int)q.size()<=m+1&&q.size()>=2) ans = max(ans, b[q.back()] - b[q.front()]);
}
if(ans == -0x3f3f3f3f) cout << 0 << "\n";
else cout << ans << "\n";
return 0;
}
今天周赛模板题居多,时间都在扣这个单调队列了,这次周赛让我认识到了,算法学习的不足,没有熟练掌握学过的,还有不少要学的,道阻且长!