codeforces 508E Arthur and Brackets

坚强是说给别人听的谎言 提交于 2020-01-30 04:45:41

题目描述:

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).

Examples

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

(())()

题目大意:

有n对合法括号,每对括号的左右括号距离有一个范围,你的任务就是构造一个这样的括号序列。如果不能的话输出:"  IMPOSSIBLE "

解题报告:

1:定义一个栈,每次输入就将一个 "("压入栈中。这里只将i压入,然后通过idx[i] = cnt将"("括号的下标存入。

2:检查  | cnt - idx[x] | 是否在区间范围内。在的话就立即补上")",并弹出栈顶元素。有点类似于贪心思想。

3:最后栈内有值就说明不能构造,反之输出括号序列。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 600+10;
stack<ll> node;
char ss[N<<1];
ll l[N], r[N], idx[N];
int main(){
    ll n, cnt = 0;
    scanf("%lld", &n);
    for(ll i=0; i<n; ++i){
        scanf("%lld%lld", l+i, r+i);
        node.push(i), idx[i] = cnt;
        ss[cnt++] = '(';
        while(!node.empty()){
            ll x = node.top();
            if(fabs(cnt - idx[x]) >= l[x] && fabs(cnt - idx[x]) <= r[x]){
                ss[cnt++] = ')', node.pop();
            }else break;
        }
    }
    if(node.empty())puts(ss);
    else puts("IMPOSSIBLE");
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!