为什么O(n)的单调队列最后一个点会TLE
查看原帖
为什么O(n)的单调队列最后一个点会TLE
359354
Beihai_Jiang楼主2024/8/30 21:31
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5;
int n,m;
int q[N],s=1,t;
int ti[N];
int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0' || ch>'9')
		ch=getchar();
	while(ch>='0' && ch<='9')
		x=10*x+ch-'0',ch=getchar();
	return x;
}
void write(int x,int opt){
	if(x>9)
		write(x/10,0);
	putchar(x%10+'0');
	if(opt==1) putchar('\n');
	return;
}
void push(int x,int i){
	while(t>=s && q[t]>x)
		t--;
	q[++t]=x;
	ti[t]=i;
}
void pop(int i){
	if(i-ti[s]+1>m)
		s++;
}
int getmin(){
	return q[s];
}
int main(){
	n=read(),m=read();
	write(0,1);
	for(int i=1;i<=n;i++){
		int x=read();
		push(x,i);
		pop(i);
		if(i<n)
			write(getmin(),1);
	}
	return 0;
}
2024/8/30 21:31
加载中...