101972024-03-29 12:21:33111Zárójelekcpp17Time limit exceeded 0/100527ms255428 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){
		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);
	}
};

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
1Accepted455ms252164 KiB
2Time limit exceeded501ms252428 KiB
subtask20/11
3Accepted456ms252664 KiB
4Accepted456ms252804 KiB
5Accepted497ms252780 KiB
6Accepted500ms252992 KiB
7Accepted456ms253528 KiB
8Time limit exceeded501ms253724 KiB
subtask30/6
9Accepted458ms253508 KiB
10Time limit exceeded501ms253756 KiB
11Time limit exceeded501ms253984 KiB
subtask40/14
12Accepted460ms254224 KiB
13Time limit exceeded501ms254300 KiB
subtask50/23
14Wrong answer458ms254452 KiB
15Time limit exceeded504ms254672 KiB
subtask60/19
16Accepted456ms254788 KiB
17Time limit exceeded504ms254708 KiB
18Wrong answer460ms254816 KiB
19Wrong answer455ms254816 KiB
20Time limit exceeded508ms254816 KiB
21Time limit exceeded504ms254880 KiB
22Wrong answer460ms255124 KiB
23Wrong answer455ms255016 KiB
24Wrong answer458ms254944 KiB
subtask70/27
25Accepted453ms255068 KiB
26Accepted456ms255072 KiB
27Wrong answer458ms255076 KiB
28Time limit exceeded504ms255184 KiB
29Time limit exceeded514ms255076 KiB
30Time limit exceeded513ms255180 KiB
31Time limit exceeded507ms255124 KiB
32Time limit exceeded504ms255124 KiB
33Time limit exceeded505ms255188 KiB
34Time limit exceeded509ms255184 KiB
35Wrong answer458ms255076 KiB
36Wrong answer467ms255084 KiB
37Time limit exceeded501ms255192 KiB
38Wrong answer458ms255176 KiB
39Time limit exceeded527ms255084 KiB
40Time limit exceeded504ms255080 KiB
41Time limit exceeded501ms255080 KiB
42Wrong answer458ms255332 KiB
43Wrong answer456ms255428 KiB