luogu P1440 求m区间内的最小值

luogu P1440 求m区间内的最小值_第1张图片

analysis

单调队列

code

#include
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define clean(arry,num) memset(arry,num,sizeof(arry))
#define anti_loop(i,start,end) for(register int i=start;i>=end;--i)
#define ll long long
template<typename T>void read(T &x){
	x=0;char r=getchar();T neg=1;
	while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
	while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
	x*=neg;
}
const int maxn=2000000+10;
class dandiaoqueue{
	public:
		struct node{
			int pos;
			int w;
		};
		node que[maxn];
		int l,r;
		void init(){clean(que,0);l=1;r=0;}//初始化注意
		void push_back(int pos,int w){
			que[++r].pos=pos;
			que[r].w=w;
		}
		void pop_back(){--r;}
		void pop_front(){++l;}
		node front(){return que[l];}
		node back(){return que[r];}
		bool empty(){return l>r;}//令l>r等价于队列为空
}duilie;
int n,m;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("datain.txt","r",stdin);
	#endif
	read(n),read(m);
	duilie.init();
	loop(i,1,n){
		int ai;
		read(ai);
		if(i==1){
			printf("0\n");
			duilie.push_back(1,ai);
			continue;
		}
		while(duilie.empty()==false&&i-duilie.front().pos>m)duilie.pop_front();
		if(duilie.empty()==false)printf("%d\n",duilie.front().w);
		else printf("%d\n",ai);
		while(duilie.empty()==false&&duilie.back().w>ai)duilie.pop_back();
		duilie.push_back(i,ai);
	}
	return 0;
}

你可能感兴趣的:(数据结构)