Codeforces Round #288 (Div. 2)E. Arthur and Brackets

94 篇文章 0 订阅
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).

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

设dp[l][r] 表示第l个左括弧到第r个左括弧是否成功匹配,
考虑第l个左括弧和第i个右括弧匹配,然后寻找子状态 (l+1, i), (i+1,r)

/*************************************************************************
    > File Name: cf288e.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年01月30日 星期五 13时29分45秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 650;
int dp[N][N];
int match[N][N];
struct node
{
	int l, r;
}seg[N];

int dfs (int l, int r)
{
	if (l > r)
	{
		return 1;
	}
	if (dp[l][r])
	{
		return dp[l][r];
	}
	for (int i = l; i <= r; ++i)
	{
		int num = (i - l) * 2;
		if (num <= seg[l].r - 1 && num >= seg[l].l - 1)
		{
			if (dfs (l + 1, i) == 1 && dfs (i + 1, r) == 1)
			{
				match[l][r] = i;
				return dp[l][r] = 1;
			}
		}
	}
	return dp[l][r] = -1;
}

void print(int l, int r)
{
	if (l > r)
	{
		return;
	}
	printf("(");
	print(l + 1, match[l][r]);
	printf(")");
	print(match[l][r] + 1, r);
}

int main ()
{
	int n;
	while (~scanf("%d", &n))
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &seg[i].l, &seg[i].r);
		}
		memset (dp, 0, sizeof(dp));
		dfs (1, n);
		if (dp[1][n] == -1)
		{
			printf("IMPOSSIBLE\n");
		}
		else
		{
			print(1, n);
			printf("\n");
		}
	}
	return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值