101962024-03-29 12:20:36111Zárójelekcpp17Time limit exceeded 0/100588ms253932 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define MZ 500000

struct ST{
	int n;
	vector<int>a,b;
	
	ST(int n):n(n),a(n*4,INT_MIN),b(n*4,-1){
	}
	
	pair<int,int>query(int i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return {INT_MIN,-1};
		}
		if(l>=ll&&r<=rr){
			return {a[i],b[i]};
		}
		auto[al,bl]=query(i*2+1,l,(l+r)/2,ll,rr);
		auto[ar,br]=query(i*2+2,(l+r)/2+1,r,ll,rr);
		return al>=ar?pair<int,int>{al,bl}:pair<int,int>{ar,br};
	}
	
	pair<int,int>query(int ll,int rr){
		auto it=max_element(a.begin()+ll,a.begin()+rr+1);
		return{*it,it-a.begin()};
		// return query(0,0,n-1,ll,rr);
	}
	
	void update(int i,int l,int r,int ii,int xx){
		if(l>ii||r<ii){
			return;
		}
		if(l==r){
			a[i]=xx;
			b[i]=l;
			return;
		}
		update(i*2+1,l,(l+r)/2,ii,xx);
		update(i*2+2,(l+r)/2+1,r,ii,xx);
		a[i]=a[i*2+1]>=a[i*2+2]?a[i*2+1]:a[i*2+2];
		b[i]=a[i*2+1]>=a[i*2+2]?b[i*2+1]:b[i*2+2];
	}
	
	void update(int ii,int xx){
		// update(0,0,n-1,ii,xx);
		a[ii]=xx;
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	ST a(MZ),b(MZ);
	vector<int>ma(MZ),mb(MZ),za(MZ),zb(MZ);
	set<int>aa,bb;
	for(int i=0;i<MZ;i++){
		aa.insert(i);
		bb.insert(i);
	}
	int x=0,c=0;
	for(int i=0;i<N;i++){
		string S;
		cin>>S;
		int y=0,z=0;
		for(char c:S){
			if(c=='('){
				y++;
			}
			else{
				y--;
				z=max(z,-y);
			}
		}
		x+=y;
		if(y>=0){
			c++;
			int zz=*aa.lower_bound(MZ/2-z);
			aa.erase(zz);
			a.update(zz,y);
			ma[zz]=i;
			za[zz]=z;
		}
		else{
			int zz=*bb.lower_bound(MZ/2-z);
			bb.erase(zz);
			b.update(zz,y+z);
			mb[zz]=i;
			zb[zz]=z;
		}
	}
	if(x!=0){
		cout<<-1<<'\n';
		return 0;
	}
	vector<int>ans;
	while(ans.size()<N){
		if(c>0){
			c--;
			auto[y,z]=a.query(MZ/2-x,MZ);
			if(y==INT_MIN){
				cout<<-1<<'\n';
				return 0;
			}
			a.update(z,INT_MIN);
			x+=y;
			ans.push_back(ma[z]);
		}
		else{
			auto[yz,z]=b.query(MZ/2-x,MZ);
			if(yz==INT_MIN){
				cout<<-1<<'\n';
				return 0;
			}
			b.update(z,INT_MIN);
			x+=yz-zb[z];
			ans.push_back(mb[z]);
		}
	}
	for(int i:ans){
		cout<<i+1<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Time limit exceeded505ms251916 KiB
2Time limit exceeded575ms126480 KiB
subtask20/11
3Accepted460ms252436 KiB
4Accepted456ms252652 KiB
5Time limit exceeded505ms252556 KiB
6Accepted462ms252568 KiB
7Accepted460ms253080 KiB
8Accepted458ms253480 KiB
subtask30/6
9Time limit exceeded577ms127156 KiB
10Time limit exceeded500ms253052 KiB
11Accepted458ms253036 KiB
subtask40/14
12Time limit exceeded577ms127660 KiB
13Time limit exceeded577ms127676 KiB
subtask50/23
14Time limit exceeded573ms127708 KiB
15Time limit exceeded573ms127812 KiB
subtask60/19
16Accepted497ms253476 KiB
17Time limit exceeded569ms127892 KiB
18Time limit exceeded564ms127908 KiB
19Time limit exceeded577ms128100 KiB
20Time limit exceeded573ms128124 KiB
21Time limit exceeded569ms128228 KiB
22Time limit exceeded573ms128104 KiB
23Time limit exceeded569ms128304 KiB
24Time limit exceeded564ms128252 KiB
subtask70/27
25Accepted483ms253932 KiB
26Time limit exceeded564ms128140 KiB
27Time limit exceeded556ms128076 KiB
28Time limit exceeded573ms128008 KiB
29Time limit exceeded560ms128208 KiB
30Time limit exceeded552ms128140 KiB
31Time limit exceeded565ms128460 KiB
32Time limit exceeded580ms128456 KiB
33Time limit exceeded577ms128408 KiB
34Time limit exceeded588ms128416 KiB
35Time limit exceeded573ms128488 KiB
36Time limit exceeded569ms128332 KiB
37Time limit exceeded577ms128532 KiB
38Time limit exceeded564ms128432 KiB
39Time limit exceeded573ms128412 KiB
40Time limit exceeded552ms128536 KiB
41Time limit exceeded587ms128712 KiB
42Time limit exceeded582ms128904 KiB
43Time limit exceeded573ms128960 KiB