Skip to content

Commit

Permalink
Finish LuoguP2627.cpp: monotonic queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-Yang committed Jul 2, 2024
1 parent a871648 commit d3bdb92
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Luogu/LuoguP2627.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// problem statement: https://www.luogu.com.cn/problem/P2627

#include <bits/stdc++.h>

constexpr int MAXN = 1e5 + 10;

using namespace std;

int n, k;
long long sum[MAXN], dp[MAXN];
deque<long long> dq;

int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) { cin >> sum[i]; sum[i] += sum[i - 1]; }
for (int i = 1; i <= n; i++) {
while (!dq.empty() && dp[i - 1] - sum[i] >= dp[dq.back()] - sum[dq.back() + 1]) { dq.pop_back(); }
dq.push_back(i - 1);
while (!dq.empty() && i - dq.front() - 1 > k) { dq.pop_front(); }
dp[i] = dp[dq.front()] - sum[dq.front() + 1] + sum[i];
if (i <= k) { dp[i] = max(dp[i], sum[i] - sum[max(0, i - k)]); }
}
cout << dp[n] << '\n';
return 0;
}

0 comments on commit d3bdb92

Please sign in to comment.