数字个数为8 队列的宽度限制为3。
输入:
8 3
1 3 -1 -3 5 3 6 7
单调递增队列:
#include<bits/stdc++.h>
#define UP(i,x,y) for(int i=x; i<=y; i++)
using namespace std;
struct node{
int value;
int index;
};
deque<node> q;
deque<node>::iterator pos;
int main()
{
int n;
int m;
cin>>n;
cin>>m;
UP(i, 1, n)
{
node t;
cin>>t.value;
t.index = i;
/*
出队
当队列中的第一个元素是窗体外的情况
*/
if(i > m)
{
if(q.front().index < (i - m + 1))
q.pop_front();
}
/*
入队
1.如果新的元素比队尾的元素小,那么队尾出队
2.反之直接入队
*/
while(q.empty() != true and t.value < q.back().value )
{
q.pop_back();
}
q.push_back(t);
for( pos = q.begin(); pos!=q.end(); ++pos)
{
cout<<(*pos).value<<" ";
}
cout<<endl;
}
return 0;
}