Pagini recente » Cod sursa (job #2312285) | Cod sursa (job #3253795) | Cod sursa (job #395644) | Cod sursa (job #495387) | Cod sursa (job #3125653)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream f1("deque.in");
ofstream f2("deque.out");
int N, K;
f1 >> N >> K;
int A[N], S = 0, d[N], front = 0, back = 0;
for (int i = 0; i < N; i++)
{
f1 >> A[i];
/// remove elements from the back of the deque that are greater than or equal to A[i]
while (back > front && A[d[back-1]] >= A[i])
{
back--;
}
/// add the current index to the back of the deque
d[back++] = i;
/// if the front index of the deque is i-K, move the front index forward
if (d[front]==i-K)
front++;
/// if we have a sequence of size K, add the value at the front index of the deque to S
if (i >= K)
S += A[d[front]];
}
f2 << S;
f1.close();
f2.close();
return 0;
}