【求助】关于时间复杂度
  • 板块学术版
  • 楼主KidA
  • 当前回复6
  • 已保存回复6
  • 发布时间2024/8/28 13:12
  • 上次更新2024/8/28 16:01:31
查看原帖
【求助】关于时间复杂度
542221
KidA楼主2024/8/28 13:12

有没有大佬帮忙分析一下这个程序的时间复杂度啊 qwq。个人感觉是 O(n)O(n) 的,但超时了两个点。

题目是 P7912。

#include<iostream>
#include<queue>
using namespace std;

const int N=2e5+5;
int n,tot,a[N];
struct Node{ int w,p; };
queue<Node> q[N];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cin>>n;
	a[0]=-1;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(a[i]!=a[i-1]) q[++tot].push((Node){a[i],i});
		else q[tot].push((Node){a[i],i});
	}
	while(1){
		int last=-1,f=0;
		for(int i=1;i<=tot;i++)
			if(!q[i].empty()&&q[i].front().w!=last)
				cout<<q[i].front().p<<' ',
				last=q[i].front().w,
				q[i].pop(),
				f=1;
		cout<<'\n';
		if(!f) break;
	}
	return 0;
}
2024/8/28 13:12
加载中...