deque
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
std::deque is a double-ended queue: you can quickly add and remove at both ends, and there's still d[i] access.
#include <deque>
std::deque<int> d = {2, 3};
d.push_front(1); // 1 2 3
d.push_back(4); // 1 2 3 4
d.pop_front(); // 2 3 4
d.front(); d.back(); d[1];
Under the hood there are several blocks of memory and a table of pointers to them, so inserting at the front doesn't move all the items. A classic use is the sliding window: keep the "candidates" in a deque and throw out the outdated ones at the front and the useless ones at the back.
Task. Read n, k and n numbers. For each window of k consecutive numbers, print the maximum
(separated by spaces). The solution is O(n): keep in the deque the indices whose values form a decreasing sequence.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.