rcpl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: Sliding Window Minimum (スライド最小値)
(dp/sliding_window_minimum.hpp)

お気持ち

Verified with

Code

#pragma once

template <class T> std::vector<T> sliding_window_minimum(const std::vector<T> &a, const int k) {
    int n = (int)a.size();
    std::vector<T> res(n - k + 1);
    std::deque<T> deq;
    for (int i = 0; i < n; i++) {
        while (!deq.empty() and a[deq.back()] >= a[i]) deq.pop_back();
        deq.push_back(i);
        if (i - k + 1 >= 0) {
            res[i - k + 1] = a[deq.front()];
            if (deq.front() == i - k + 1) deq.pop_front();
        }
    }
    return res;
}
#line 2 "dp/sliding_window_minimum.hpp"

template <class T> std::vector<T> sliding_window_minimum(const std::vector<T> &a, const int k) {
    int n = (int)a.size();
    std::vector<T> res(n - k + 1);
    std::deque<T> deq;
    for (int i = 0; i < n; i++) {
        while (!deq.empty() and a[deq.back()] >= a[i]) deq.pop_back();
        deq.push_back(i);
        if (i - k + 1 >= 0) {
            res[i - k + 1] = a[deq.front()];
            if (deq.front() == i - k + 1) deq.pop_front();
        }
    }
    return res;
}
Back to top page