单调队列全部RE求助
查看原帖
单调队列全部RE求助
432183
JoeBiden2020楼主2021/10/5 11:26
#include <bits/stdc++.h>
#define pass
#define GrandTheftAutoV int
using namespace std;
const int maxn=1e6+1;
int n,k;
struct s{
	int d,v;
}a[maxn];
deque <s>q;
void up(){
	for(GrandTheftAutoV i=1;i<=n;i++){
		while(!q.empty()&&a[i].v>=q.front().v){
			q.pop_front();
		}
		q.push_front(a[i]);
		while(q.back().d<=i-k)q.pop_back();
		if(i>=k)cout<<q.back().v<<" ";
	}
	puts("");
}
void down(){
	for(GrandTheftAutoV i=1;i<=n;i++){
		while(!q.empty()&&a[i].v<=q.front().v){
			q.pop_front();
		}
		q.push_front(a[i]);
		while(q.back().d>i-k)q.pop_back();
		if(i>=k)cout<<q.back().v<<" ";
	}
}
void clear(){
	while(!q.empty())q.pop_front();
}
void read(){
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(GrandTheftAutoV i=1;i<=n;i++){
		cin>>a[i].v;
		a[i].d=i;
	}
}
int main(){
	read();
	up();
	clear();
	down();
	return 0;
}
2021/10/5 11:26
加载中...