Codeforces 508E Arthur and Brackets【贪心】

来源:互联网 发布:淘宝图片软件 编辑:程序博客网 时间:2024/05/22 08:01

E. Arthur and Brackets
time limit per test
2 seconds
memory limit per test
128 megabytes
input
standard input
output
standard 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).

Examples
input
41 11 11 11 1
output
()()()()
input
35 53 31 1
output
((()))
input
35 53 32 2
output
IMPOSSIBLE
input
32 31 41 4
output
(())()

题目大意:


需要构造出来一个长度为2*n的括号匹配串,使得从左到右,每个左括号的匹配右括号在区间【Li,Ri】之间。


思路:


用一个栈模拟一下贪心就行。

我们先使得后边的左括号匹配上,再匹配前边的就行了。


Ac代码:

#include<stdio.h>#include<string.h>#include<stack>using namespace std;int l[1500000];int r[1500000];int pos[1500000];char ans[1500000];int main(){    stack<int>s;    int n;    while(~scanf("%d",&n))    {        int flag=0;        int cnt=0;        stack<int>s;        for(int i=1;i<=n;i++)        {            scanf("%d%d",&l[i],&r[i]);            s.push(i);            pos[i]=cnt;            ans[cnt++]='(';            while(!s.empty())            {                int u=s.top();                if(pos[u]+l[u]>cnt)break;                if(pos[u]+r[u]<cnt)                {                    flag=1;                    break;                }                ans[cnt++]=')';                s.pop();            }        }        if(s.size()==0&&flag==0)printf("%s\n",ans);        else printf("IMPOSSIBLE\n");    }}







原创粉丝点击