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!
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.
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).
4 1 1 1 1 1 1 1 1
()()()()
3 5 5 3 3 1 1
((()))
3 5 5 3 3 2 2
IMPOSSIBLE
3 2 3 1 4 1 4
(())()
考虑第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;
}

本文探讨了括号匹配问题,特别是在给定每个左括号对应右括号距离范围的情况下,如何通过动态规划算法来判断并构造合法的括号序列。提供了具体的算法实现,并通过样例展示了算法的有效性。
1352

被折叠的 条评论
为什么被折叠?



