70tps TLE预估复杂度O(nlng(n))
查看原帖
70tps TLE预估复杂度O(nlng(n))
1067416
wlx2012楼主2024/9/28 00:44

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[200005];
vector<int> p,p1;
vector<int> q,q1;
int f(int x,vector<int> s){ //在s中查找比x大的第一个数
	int l = -1,r = s.size(),mid;
	while(l+1<r){
		mid=(l+r)/2;
		if(s[mid]>x){
			r=mid;
		} 
		else{
			l=mid;
		}
	}
	return r;
} 

int re(){  //快读
	char ch=getchar();
	int sum=0;
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		sum=sum*10+ch-'0';
		ch=getchar();
	}
	return sum;
}
signed main(){
	n=re();
	for(int i = 1;i<=n;i++){
		a[i]=re();
		if(a[i]==1){
			p.push_back(i);
		}
		else{
			q.push_back(i);
		}
	}
	for(;p.size()+q.size()!=0;){
		p1.clear();
		q1.clear();
		int now;
		int b;//1为q,0为p 
		if(p.size()==0){
			now=q[0];
			b=1;
			q1.push_back(1);
		}
		else if(q.size()==0){
			now=p[0];
			b=0;
			p1.push_back(1);
		}
		else{
			if(q[0]<p[0]){
				now=q[0];
				b=1;
				q1.push_back(1);
			}
			else{
				now=p[0];
				b=0;
				p1.push_back(1);
			}
		}
		cout<<now<<" ";
		for(;;){
			b=1-b;
			if(b==1){
				if(q.size()==0){
					break; 
				}
				if(f(now,q)>=q.size()){
					break;
				}
				now=q[f(now,q)];
				q1.push_back(f(now,q));
			}	
			else{
				if(p.size()==0){
					break; 
				}
				if(f(now,p)>=p.size()){
					break;
				}
				now=p[f(now,p)];
				p1.push_back(f(now,p));
			}
			cout<<now<<" ";
		}
		cout<<endl;
		if(q1.size()!=0){
			for(int i = q1.size()-1;i>=0;i--){
				q.erase(q.begin()+q1[i]-1);
			}
		}
		if(p1.size()!=0){
			for(int i = p1.size()-1;i>=0;i--){
				p.erase(p.begin()+p1[i]-1);
			}
		}
	}
	return 0;
}
2024/9/28 00:44
加载中...