Nu aveti permisiuni pentru a descarca fisierul grader_test8.in
Cod sursa(job #3211193)
Utilizator | Data | 8 martie 2024 18:21:11 | |
---|---|---|---|
Problema | Deque | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.11 kb |
#pragma GCC optimize("O3,unroll-loops")
#include <fstream>
#include <iostream>
#include <deque>
using namespace std;
using ll = long long;
int fp() {
int ch, t = 1, n = 0;
while ((ch = getchar_unlocked()) && !isdigit(ch) && ch != '-');
if (ch == '-')
t = -1;
if (isdigit(ch))
n = ch - '0';
while ((ch = getchar_unlocked()) && isdigit(ch))
n = n * 10 + ch - '0';
return t * n;
}
int main() {
freopen("deque.in", "r", stdin);
ofstream g("deque.out");
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = fp(), k = fp();
deque<pair<int, int>> Q;
for (int i = 0, x; i < k; ++i) {
x = fp();
while (!Q.empty() && Q.back().first > x)
Q.pop_back();
Q.emplace_back(x, i);
}
ll sum = Q.front().first;
for (int i = k, x; i < n; ++i) {
x = fp();
while (!Q.empty() && Q.front().second <= i - k)
Q.pop_front();
while (!Q.empty() && Q.back().first > x)
Q.pop_back();
Q.emplace_back(x, i);
sum += Q.front().first;
}
g << sum << '\n';
}