Cod sursa(job #2756993)
| Utilizator | Data | 3 iunie 2021 15:31:14 | |
|---|---|---|---|
| Problema | Deque | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.48 kb |
#include <iostream>
#define maxn 5000010
ifstream f("deque.in");
ofstream g("deque.out");
int n, k;
int A[maxn], Deque[maxn];
int Front, Back;
long long Sum;
int main() {
f >> n >> k;
for (int i = 1; i <= n; i++)
f >> A[i];
Front = 1, Back = 0;
for (int i = 1; i <= n; i++) {
while (Front <= Back && A[i] <= A[ Deque[Back] ]) Back--;
Deque[++Back] = i;
if (Deque[Front] == i-k) Front++;
if (i >= k) Sum += A[ Deque[Front]];
}
g << Sum;
return 0;
}