双链表80分求助
查看原帖
双链表80分求助
754006
mobaiawa楼主2023/11/1 11:55
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node {
	int l,r,dat,pre,nex;
	bool prf,nef,fl;
}box[N];
int l;
void del_(int l){
	int pr=box[l].pre;
	int ne=box[l].nex;
	if(box[pr].fl!=1){
		box[pr].nex=ne;
		box[pr].nef=(box[l].nef==box[l].prf)?1:0;
	}
	if(box[ne].fl!=1){
		box[ne].pre=pr;
		box[ne].prf=(box[l].nef==box[l].prf)?1:0;
	}
}
int main(){
	int n;
	scanf("%d",&n);
	int l_=0,pre=-1;
	box[1].pre=0;
	box[0].fl=1;
	for(int i=1;i<=n;i++){
		int a;
		scanf("%d",&a);
		if(pre!=-1&&a!=pre){
			box[++l].l=i-l_;
			box[l].r=i-1;
			box[l].dat=l_;
			box[l].nex=l+1;
			box[l+1].pre=l;
			l_=0;
		}
		pre=a;
		l_++;
	}
	box[++l].l=n-l_+1;
	box[l].r=n;
	box[l].dat=l_;
	box[l+1].fl=1;
	int hh=1;
	while(box[hh].dat){
		for(int i=hh;!box[box[i].nex].fl;i=box[i].nex){
			if(box[i].dat==0){
				del_(i);
			}
		}
		for(int i=hh;!box[i].fl;i=box[i].nex){
			if((box[i].prf==0||box[box[i].pre].fl)&&box[i].dat){
				cout<<box[i].l<<" ";
				box[i].l++;
				box[i].dat--;
			}
		}
		cout<<endl;
		if(box[hh].dat==0){
			while(box[box[hh].nex].fl!=1&&box[hh].dat==0){
				del_(hh);
				hh=box[hh].nex;
			}
		}
	}
	return 0;
}

这是拿数组模拟的,原来我以为newnodenew node卡超时了结果还没过

原来的代码

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node {
	int l,r,dat;
	bool prf,nef,fl;
	node *pre,*nex;
	
}*hea=new node;
void del_(node *d){
	node *pr=d->pre;
	node *ne=d->nex;
		if(pr->fl!=1){pr->nex=ne;pr->nef=(d->nef==d->prf)?1:0;}
		if(ne->fl!=1){ne->pre=pr;ne->prf=(d->nef==d->prf)?1:0;}
}
int main(){
	int n;
	scanf("%d",&n);
	int l=0,pre=-1;
	hea->pre=new node;
	hea->pre->fl=1;
	node *_=hea;
	for(int i=1;i<=n;i++){
		int a;
		scanf("%d",&a);
		if(pre!=-1&&a!=pre){
			_->l=i-l;
			_->r=i-1;
			_->dat=l;
	        _->nex=new node;
			_->nex->pre=_;
			_=_->nex;
			l=0;
		}
		pre=a;l++;
	}
	_->l=n-l+1;
	_->r=n;
	_->dat=l;
	_->nex=new node;
	_->nex->fl=1;
	node *hh=hea;
	while(hh->dat){
		for(node *i=hh;!i->nex->fl;i=i->nex){
			if(i->dat==0)del_(i);
		}
		for(node *i=hh;!i->fl;i=i->nex){
			if((i->prf==0||i->pre->fl)&&i->dat){
				cout<<i->l<<" ";
				i->l++;
				i->dat--;
			}
		}
		cout<<endl;
		if(hh->dat==0)while(hh->nex->fl!=1&&hh->dat==0)del_(hh),hh=hh->nex;
	}
	return 0;
}

这代码很迷来着

2023/11/1 11:55
加载中...