50WA,玄关
查看原帖
50WA,玄关
1284088
meifan666楼主2025/1/4 22:46
#include<bits/stdc++.h>//队列加链表(想写双链表可惜码力不够)
using namespace std;
#define int long long
int n,a[200100],t,cnt;
int nex[200100],to[200100];
struct kuai{
	int sta,fin;
}k[200100];
bool vis[200100];
queue<int>p1,p2;
signed main(){
	scanf("%lld",&n);
	t=n;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		if(i==1||a[i]!=a[i-1]){
			if(i>1){
				cnt++;
				k[cnt]={k[cnt-1].fin+1,i-1};
			}
			p1.push(i);
		}
		nex[i]=i+1;
	}
	cnt++;
	k[cnt]={k[cnt-1].fin+1,n};
	for(int i=1;i<=cnt;i++){
		for(int j=k[i].sta;j<=k[i].fin;j++){
			to[j]=i;
		}
	}
	while(!p1.empty()){
		while(!p1.empty()){
//			printf("%lld  ",nex[p1.front()]);
			printf("%lld ",p1.front());
			vis[p1.front()]=1;
//			--n;
			p2.push(nex[p1.front()]);
			p1.pop();
		}
		int tt=-1;
//		cout<<'\n';	
		while(!p2.empty()){
			while(!p2.empty()&&vis[p2.front()])p2.pop();
			if(p2.empty())break;
			if(p2.front()==0||p2.front()>t){
				p2.pop();
				continue;
			}
			if(tt==-1){
				tt=to[p2.front()];
				p1.push(p2.front());
			}else if(a[p2.front()]==a[k[tt].fin]){
				nex[k[tt].fin]=p2.front();
				k[tt].fin=k[to[p2.front()]].fin;
				tt=to[p2.front()];
			}else{
				tt=to[p2.front()];
				p1.push(p2.front());
			}
			p2.pop();
		}
		puts("");
	}
	return 0;
}
2025/1/4 22:46
加载中...