P2032 扫描

原题链接

今天刚学STL

发个板子纪念一下(单调队列)

#include<bits/stdc++.h>
using namespace std;
int n,k;
struct node{
    int x,y;
};
deque<node> dq;

int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        node a;
        cin>>a.x;
        a.y=i;
        while(!dq.empty() && a.x>=dq.back().x)
        dq.pop_back();
        dq.push_back(a);
        if(!dq.empty() && i-dq.front().y>k-1)
        dq.pop_front();
        if(i>=k)
        cout<<dq.front().x<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhouzhihao/p/10806095.html