# Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp

            E. Arthur and Brackets


time limit per test2 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard output
Notice that the memory limit is non-standard.

Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur’s favorite correct bracket sequence just to spite him.

All Arthur remembers about his favorite sequence is for each opening parenthesis (‘(‘) the approximate distance to the corresponding closing one (‘)’). For the i-th opening bracket he remembers the segment [li, ri], containing the distance to the corresponding closing bracket.

Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li, ri].

Help Arthur restore his favorite correct bracket sequence!

Input
The first line contains integer n (1 ≤ n ≤ 600), the number of opening brackets in Arthur’s favorite correct bracket sequence.

Next n lines contain numbers li and ri (1 ≤ li ≤ ri < 2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one.

The descriptions of the segments are given in the order in which the opening brackets occur in Arthur’s favorite sequence if we list them from left to right.

Output
If it is possible to restore the correct bracket sequence by the given data, print any possible choice.

If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line “IMPOSSIBLE” (without the quotes).

Sample test(s)
input
4
1 1
1 1
1 1
1 1
output
()()()()
input
3
5 5
3 3
1 1
output
((()))
input
3
5 5
3 3
2 2
output
IMPOSSIBLE
input
3
2 3
1 4
1 4
output
(())()

#define N 1605
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,l[N],r[N],p[N],ansi;
char ans[N + N];
stack<int> st;
int main()
{
while(S(n)!=EOF)
{
FI(n) {
S2(l[i],r[i]);
}
while(!st.empty()) st.pop();
bool flag = true;
ansi = 0;
for(int i=0;i<n && flag;i++){
st.push(i);
p[i] = ansi;
ans[ansi++] = '(';
while(flag && !st.empty() && p[st.top()] + l[st.top()] <= ansi){
int k = st.top();
if(p[k] + r[k] >= ansi){
ans[ansi++] = ')';
}
else {
flag = false;
break;
}
st.pop();
}
}
ans[ansi] = '\0';
if(!flag || strlen(ans) != (n + n)){
printf("IMPOSSIBLE\n");
}
else {
puts(ans);
}
}
return 0;
}


dp[i][j] 表示 从每i个括号到第j个括号是否可以生成符合要求的序列

dp[i][k] ,dp[k+1][j],（表求这样的序列 ‘（。。。）（。。。）’）

#define N 605
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,l[N],r[N],ansi,dp[N][N];
char ans[N + N];
bool CheckIsRight(int len,int s){
if(len>=l[s] && len<= r[s])
return true;
return false;
}
int DFS(int s,int e,int pos){
if(dp[s][e] != -1) return dp[s][e];
if(s == e){
if(CheckIsRight(1,s)){
ans[pos] = '(';ans[pos +1] = ')';
return dp[s][e] = 1;
}
else
return dp[s][e] = 0;
}
for(int i=s;i<e;i++){
if(CheckIsRight(2*(i - s + 1) - 1,s) && DFS(s,i,pos) &&
DFS(i+1,e,pos + 2*(i-s+1))) {
ans[pos] = '(';ans[pos + 2*(i - s + 1) - 1] = ')';
return dp[s][e] = 1;
}
}
if(CheckIsRight(2 * (e-s) + 1,s) && DFS(s+1,e,pos+1)){
ans[pos] = '(';ans[pos + 2 * (e-s) + 1] = ')';
return dp[s][e] = 1;
}
return dp[s][e] = 0;
}
int main()
{
while(S(n)!=EOF)
{
FI(n) {
S2(l[i],r[i]);
}
memset(dp,-1,sizeof(dp));
if(DFS(0,n-1,0)){
ans[n+n] = '\0';
puts(ans);
}
else {
puts("IMPOSSIBLE");
}
}
return 0;
}

• 本文已收录于以下专栏：

## Codeforces Round #288 (Div. 2)---D. Tanya and Password

D. Tanya and Password time limit per test 2 seconds memory limit per test 256 megabytes i...

## Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索

D. Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes ...

## E. Vanya and Brackets（Codeforces Round #308 (Div. 2)）

E. Vanya and Brackets time limit per test 1 second memory limit per test 256 megabytes ...

## Codeforces Round #343 (Div. 2) C. Famil Door and Brackets（dp）

• lwt36
• 2016年02月26日 23:09
• 194

## Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation（贪心）

Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to ...

举报原因： 您举报文章：Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)