TLE求调!
查看原帖
TLE求调!
1343308
IAKIOI66666楼主2024/9/7 22:08

前5个数据点AC,后面的TLE

#include<bits/stdc++.h>
using namespace std;
struct Linked{
	int last,next;
	bool value;
}a[500000];
int n,aa,len,cnt,head=1;
void del(int x)
{
	int le=a[x].last,rt=a[x].next;
	a[le].next=rt;a[rt].last=le;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&aa);
		a[i].last=i-1,a[i].next=i+1,a[i].value=aa;
	}
	while(a[1].next!=0)
	{
		int now=head;
		a[n].next=0,cnt=0;
		bool b=(a[head].value+1)%2;
		while(now)
		{
			if(a[now].value!=b)
			{
				if(head==now&&a[a[now].next].next==0)
				{
					printf("%d ",now);
					if(a[now].value==a[a[now].next].value)puts("");
					printf("%d",a[now].next);
					a[1].next=0;break;
				}
				if(!cnt&&a[a[now].next].value!=b){cnt=1,head=a[now].next;}
				printf("%d ",now);
				del(now),b=a[now].value;
			}
			now=a[now].next;
		}
		cout<<endl;
	}
    return 0;
}
2024/9/7 22:08
加载中...