10pts,求大佬看一眼
查看原帖
10pts,求大佬看一眼
781131
suxiaozhou楼主2024/1/6 22:36

1~8点WA,9号点RE

使用队列

nxt单链表链接两个在模拟中相连的同样水果的"块"

#include <bits/stdc++.h>
using namespace std;

int n,nxt[200010];
struct node{
	bool type;
	int value,front_,times=0;
}tmpnode;
queue <node> q;
int main(){
	ios::sync_with_stdio(0);
	cin>>n;
	for (int i=1;i<=n+1;i++){
		nxt[i]=i+1;
	}
	int lst=-1,tmp,v=0,cnt=0;
	while (n--){
		++cnt;
		cin>>tmp;
		if (tmp==lst||cnt==1){
			v++;
		}
		else{
			tmpnode.type=!tmp;
			tmpnode.value=v;
			tmpnode.front_=cnt-v;
			//cout<<tmpnode.type<<" "<<tmpnode.value<<" "<<tmpnode.front_<<endl;
			q.push(tmpnode);
			v=1;
		}
		lst=tmp;
	}
	tmpnode.type=lst;
	tmpnode.value=v;
	tmpnode.front_=cnt-v+1;
	//cout<<tmpnode.type<<" "<<tmpnode.value<<" "<<tmpnode.front_<<endl;
	q.push(tmpnode);
	//
	lst=0;
	while (!q.empty()){
		node now=q.front();
		q.pop();
		if (lst<now.times){
			cout<<endl;
		}
		if (!q.empty()){
			while (q.front().type==now.type){
				nxt[now.front_+now.value-1]=q.front().front_;
				now.value+=q.front().value;
				q.pop();
			}
		}
		now.value--;
		cout<<now.front_<<" ";
		now.front_=nxt[now.front_];
		lst=now.times;
		if (now.value<=0){
			continue;
		}
		now.times++;
		q.push(now);
	}
	return 0;
}

谢谢!

2024/1/6 22:36
加载中...