mxqz
查看原帖
mxqz
400783
Nephren_Sakura楼主2021/12/24 23:09

求助,复杂度正确O(n),1WA2TLE

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,head,tail;
struct node{
	int tim,zhi;
}a[2000005];
signed main(){
	cin>>n>>m;
	head=1,tail=0;
	for(int i=1; i<=n; i++){
		int x;
		cin>>x;
		while(head<=tail&&a[tail].zhi>=x)
		    tail--;  
		while(head<=tail&&a[head].tim<i-m)
		    head++;
		if(i==1)
		    cout<<"0\n";
		else
		    cout<<a[head].zhi<<endl;
		a[++tail].zhi=x;
		a[tail].tim=i;
	}
    return 0;
}

2021/12/24 23:09
加载中...