现在的位置: 首页 > 综合 > 正文

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

2019年02月15日 ⁄ 综合 ⁄ 共 2866字 ⁄ 字号 评论关闭
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;
}

抱歉!评论已关闭.