Deque-STL

  • + 0 comments

    include

    include

    include

    include

    using namespace std;

    void printMaxInSubarrays(const deque& dq, int k) {

    deque<int> indices; // To store indices of potential maximums
    
    for (int i = 0; i < dq.size(); ++i) {
        // Remove indices that are out of the current window
        while (!indices.empty() && indices.front() <= i - k) {
            indices.pop_front();
        }
    
        // Remove elements that are less than the current element
        while (!indices.empty() && dq[indices.back()] <= dq[i]) {
            indices.pop_back();
        }
    
        // Add current element's index at the end of the deque
        indices.push_back(i);
    
        // The first k-1 windows will not be complete, so skip printing max
        if (i >= k - 1) {
            cout << dq[indices.front()] << " ";
        }
    }
    cout << endl;
    

    }

    int main(){

    int t,s,k;
    cin>>t;
    
    vector<deque<int> > all_deques;
    
    
    
    for(int i=0;i<t;i++){
        cin>>s>>k;
    
        deque<int> my_deque;
    
        for(int i=0;i<s;i++){
            int value;
            cin>>value;
            my_deque.push_back(value);
        }
    
        all_deques.push_back(my_deque);
    
    }
    
    
    
        for(const auto& my_deque : all_deques){
            printMaxInSubarrays(my_deque,k);
        }
    
    
    
    
    return 0;
    

    }