求算时间复杂度
查看原帖
求算时间复杂度
134066
Pethly_Cat楼主2021/10/24 11:10

考场上写了个双向链表,本地测 n=2e5n=2e5 的数据 1.4s ,不知道能不能卡过去。。。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,a[N],l[N],r[N];
int main(){
//	freopen("fruit.in","r",stdin);
//	freopen("fruit.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		l[i]=i-1,r[i]=i+1;
		a[i]++;
	}
	r[0]=1;
	int cnt=0;
	while(cnt<n){
		int pre=0;
		for(int i=r[0];i<=n;i=r[i])
			if(a[i]!=a[pre]){
				printf("%d ",i);
				pre=i;
				r[l[i]]=r[i];
				l[r[i]]=l[i];
				cnt++;
			}
		puts("");
	}
	return 0;
}
2021/10/24 11:10
加载中...