#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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Time limit exceeded | 505ms | 251916 KiB | ||||
2 | Time limit exceeded | 575ms | 126480 KiB | ||||
subtask2 | 0/11 | ||||||
3 | Accepted | 460ms | 252436 KiB | ||||
4 | Accepted | 456ms | 252652 KiB | ||||
5 | Time limit exceeded | 505ms | 252556 KiB | ||||
6 | Accepted | 462ms | 252568 KiB | ||||
7 | Accepted | 460ms | 253080 KiB | ||||
8 | Accepted | 458ms | 253480 KiB | ||||
subtask3 | 0/6 | ||||||
9 | Time limit exceeded | 577ms | 127156 KiB | ||||
10 | Time limit exceeded | 500ms | 253052 KiB | ||||
11 | Accepted | 458ms | 253036 KiB | ||||
subtask4 | 0/14 | ||||||
12 | Time limit exceeded | 577ms | 127660 KiB | ||||
13 | Time limit exceeded | 577ms | 127676 KiB | ||||
subtask5 | 0/23 | ||||||
14 | Time limit exceeded | 573ms | 127708 KiB | ||||
15 | Time limit exceeded | 573ms | 127812 KiB | ||||
subtask6 | 0/19 | ||||||
16 | Accepted | 497ms | 253476 KiB | ||||
17 | Time limit exceeded | 569ms | 127892 KiB | ||||
18 | Time limit exceeded | 564ms | 127908 KiB | ||||
19 | Time limit exceeded | 577ms | 128100 KiB | ||||
20 | Time limit exceeded | 573ms | 128124 KiB | ||||
21 | Time limit exceeded | 569ms | 128228 KiB | ||||
22 | Time limit exceeded | 573ms | 128104 KiB | ||||
23 | Time limit exceeded | 569ms | 128304 KiB | ||||
24 | Time limit exceeded | 564ms | 128252 KiB | ||||
subtask7 | 0/27 | ||||||
25 | Accepted | 483ms | 253932 KiB | ||||
26 | Time limit exceeded | 564ms | 128140 KiB | ||||
27 | Time limit exceeded | 556ms | 128076 KiB | ||||
28 | Time limit exceeded | 573ms | 128008 KiB | ||||
29 | Time limit exceeded | 560ms | 128208 KiB | ||||
30 | Time limit exceeded | 552ms | 128140 KiB | ||||
31 | Time limit exceeded | 565ms | 128460 KiB | ||||
32 | Time limit exceeded | 580ms | 128456 KiB | ||||
33 | Time limit exceeded | 577ms | 128408 KiB | ||||
34 | Time limit exceeded | 588ms | 128416 KiB | ||||
35 | Time limit exceeded | 573ms | 128488 KiB | ||||
36 | Time limit exceeded | 569ms | 128332 KiB | ||||
37 | Time limit exceeded | 577ms | 128532 KiB | ||||
38 | Time limit exceeded | 564ms | 128432 KiB | ||||
39 | Time limit exceeded | 573ms | 128412 KiB | ||||
40 | Time limit exceeded | 552ms | 128536 KiB | ||||
41 | Time limit exceeded | 587ms | 128712 KiB | ||||
42 | Time limit exceeded | 582ms | 128904 KiB | ||||
43 | Time limit exceeded | 573ms | 128960 KiB |