From 173c882d8e84aafe05f9c9b232c0f5eba71ceb94 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 23 Jun 2022 11:32:55 +0800 Subject: [PATCH] P3594 [POI2015] WIL https://www.luogu.com.cn/record/77767878 --- Luogu/P3594/P3594.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Luogu/P3594/P3594.cpp diff --git a/Luogu/P3594/P3594.cpp b/Luogu/P3594/P3594.cpp new file mode 100644 index 00000000..1f5e21b1 --- /dev/null +++ b/Luogu/P3594/P3594.cpp @@ -0,0 +1,47 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e6 + 5; + +int n, d, a[N], ans; +long long p, sum, s[N], t[N]; +std::deque q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> p >> d; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + s[i] = s[i - 1] + a[i]; + } + + for (int i = 1; i <= n; i++) { + t[i] = s[i] - s[i - d]; + } + + ans = d; + sum = s[d + 1]; + q.push_back(d); + + for (int i = d + 1, l = 1, r = 0; i <= n; sum += a[++i]) { + while (!q.empty() && t[q.back()] <= t[i]) q.pop_back(); + q.push_back(i); + while (sum - t[q.front()] > p) { + if (q.front() < l + d) q.pop_front(); + sum -= a[l++]; + } + ans = std::max(ans, i - l + 1); + } + + cout << ans << endl; + + return 0; +}