import collections monotonous_queue = collections.deque() classItem(object): def__init__(self, i, v): self.index = i self.value = v
items: tuple[Item] = tuple()
deffront(): return monotonous_queue[0]
defback(): return monotonous_queue[-1]
for i inrange(0, n): ifnot monotonous_queue.empty() and top().index <= i - length: monotonous_queue.leftpop() t = items[i] whilenot monotonous_queue.empty() and back().value < t.value: monotonous_queue.pop() monotonous_queue.push(t)
#include<deque> structItem{ size_t index; int value; } items[n]; deque<Item> monotonous_queue; for(size_t i = 0; i < n; i++){ if(!monotonous_queue.empty() && monotonous_queue.front().index <= i - length) monotonous_queue.pop_front(); while(!monotonous_queue.empty() && monotonous_queue.back().value < items[i].value) monotonous_queue.pop_back(); monotonous_queue.push_back(items[i]); }