-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path508E.cpp
82 lines (77 loc) · 2.04 KB
/
508E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Rodrigo Farias de Macêdo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define SZ(S) ((int) ((S).k()))
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
int n, dp[610][610], st[610][610], l[610], r[610];
int sol[1220];
void solve(int a,int b){
if(a==b) printf("()");
else if(dp[a][b]==a){
if(st[a][b]==1){
printf("()");
solve(a+1,b);
}
else{
printf("(");
solve(a+1,b);
printf(")");
}
}
else{
solve(a,dp[a][b]);
solve(dp[a][b]+1,b);
}
}
int main(){
ios :: sync_with_stdio(false);
int n;
cin>>n;
REP(i,n){
cin>>l[i]>>r[i];
}
REP(i,n){
if(l[i]==1)
st[i][i]=1;
}
FOR(k, 2, n){
REP(i, n){
FOR(j, i, i+k-1){
if(j==i){
if(l[i]==1 && st[j+1][i+k-1]>0){
dp[i][i+k-1]=j;
st[i][i+k-1]=1;
break;
}
if(l[i]<=2*k-1 && r[i]>=2*k-1 && st[j+1][i+k-1]>0){
dp[i][i+k-1]=j;
st[i][i+k-1]=2;
break;
}
}
else{
if(st[i][j]>0 && st[j+1][i+k-1]>0){
dp[i][i+k-1]=j;
st[i][i+k-1]=1;
break;
}
}
}
}
}
if(st[0][n-1]==0){
cout<<"IMPOSSIBLE"<<endl;
}
else{
solve(0,n-1);
printf("\n");
}
return 0;
}