-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish LuoguP2627.cpp: monotonic queue
- Loading branch information
1 parent
a871648
commit d3bdb92
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |