Queries with Fixed Length

  • + 0 comments

    It can be optimized with dp, but since it is with deque...

    int Solve(vector<int> arr, int d)
    {
        int mn, mx;
        deque<int> mmi;
        mmi.push_back(0);
        for(int i = 1; i < d; mmi.push_back(i), i++)
            while(!mmi.empty() && arr[mmi.back()] < arr[i])
                mmi.pop_back();
        mn = mx = mmi.front();
        for(int i = d; i < arr.size(); i++)
        {
            if(mx == i - d)
                mmi.pop_front();
            while(!mmi.empty() && arr[mmi.back()] <= arr[i])
                mmi.pop_back();
            mmi.push_back(i);
            mx = mmi.front();
            mn = arr[mx] < arr[mn] ? mx : mn;
        }
    
        return arr[mn];
    }