Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp

原创 2015年07月09日 17:29:46
            E. Arthur and Brackets

time limit per test2 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard 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
(())()
题意要求生成括号序列,每个开括号与相对应的闭括号的距离不超过给定的值,用栈来保存当前要匹配的下标,如果当前这个要匹配的能匹配就直接匹配,因为ansi这个下标是连续增长的,这里匹配了对以后要匹配的值没有影响,相反,如果左端能匹配,但是右端不能匹配,则在以后,右端都一定不能匹配(因为ansi下标的递增),所以直接输出错误就可以了!

#define N 1605
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,l[N],r[N],p[N],ansi;
char ans[N + N];
stack<int> st;
int main()
{
    while(S(n)!=EOF)
    {
        FI(n) {
            S2(l[i],r[i]);
        }
        while(!st.empty()) st.pop();
        bool flag = true;
        ansi = 0;
        for(int i=0;i<n && flag;i++){
            st.push(i);
            p[i] = ansi;
            ans[ansi++] = '(';
            while(flag && !st.empty() && p[st.top()] + l[st.top()] <= ansi){
                int k = st.top();
                if(p[k] + r[k] >= ansi){
                    ans[ansi++] = ')';
                }
                else {
                    flag = false;
                    break;
                }
                st.pop();
            }
        }
        ans[ansi] = '\0';
        if(!flag || strlen(ans) != (n + n)){
            printf("IMPOSSIBLE\n");
        }
        else {
            puts(ans);
        }
    }
    return 0;
}

方法二,使用区间dp,也就是记忆化搜索
dp[i][j] 表示 从每i个括号到第j个括号是否可以生成符合要求的序列
状态转移方程 dp[i][j] = dp[i+1][j] (表示这样的序列‘(。。。)’)
dp[i][k] ,dp[k+1][j],(表求这样的序列 ‘(。。。)(。。。)’)
复杂度为o(n^3),由于n较小可以过,上面贪心的方法是o(n)方法。

#define N 605
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,l[N],r[N],ansi,dp[N][N];
char ans[N + N];
bool CheckIsRight(int len,int s){
    if(len>=l[s] && len<= r[s])
        return true;
    return false;
}
int DFS(int s,int e,int pos){
    if(dp[s][e] != -1) return dp[s][e];
    if(s == e){
        if(CheckIsRight(1,s)){
            ans[pos] = '(';ans[pos +1] = ')';
            return dp[s][e] = 1;
        }
        else
            return dp[s][e] = 0;
    }
    for(int i=s;i<e;i++){
        if(CheckIsRight(2*(i - s + 1) - 1,s) && DFS(s,i,pos) &&
           DFS(i+1,e,pos + 2*(i-s+1))) {
               ans[pos] = '(';ans[pos + 2*(i - s + 1) - 1] = ')';
               return dp[s][e] = 1;
           }
    }
    if(CheckIsRight(2 * (e-s) + 1,s) && DFS(s+1,e,pos+1)){
        ans[pos] = '(';ans[pos + 2 * (e-s) + 1] = ')';
        return dp[s][e] = 1;
    }
    return dp[s][e] = 0;
}
int main()
{
    while(S(n)!=EOF)
    {
        FI(n) {
            S2(l[i],r[i]);
        }
        memset(dp,-1,sizeof(dp));
        if(DFS(0,n-1,0)){
            ans[n+n] = '\0';
            puts(ans);
        }
        else {
            puts("IMPOSSIBLE");
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

DFS Codeforces505D Mr. Kitayuta's Technology

传送门:点击打开链接 题意:告诉你n个点,和m条关系,每个关系有u和v,要求u能通过有向边到达v,问要完成这个有向图至少需要多少条边。 思路:就是个xjb的贪心....首先,我们把边按无向图处理,...

Codeforces Round #288 (Div. 2)---D. Tanya and Password

D. Tanya and Password time limit per test 2 seconds memory limit per test 256 megabytes i...

E. Vladik and cards Codeforces Round #384 (Div. 2) 好题 二分+(贪心+状态压缩DP)判断

题意:给一串取值为1-8的序列,找出满足下列要求的最长子序列(不要求连续): 1.每个值出现的次数相差不超过1; 2.子序列中相同的值得出现在一起。 设满足要求的子序列数字出现次数至少为len。...

Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索

D. Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes ...

E. Vanya and Brackets(Codeforces Round #308 (Div. 2))

E. Vanya and Brackets time limit per test 1 second memory limit per test 256 megabytes ...

Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(dp)

题意: 给定M≤N≤105,M为原始括号序列s的长度,现要求寻找p,q括号序列给定M\le N \le 10^5, M为原始括号序列s的长度, 现要求寻找p, q括号序列 要求|p+s+q|...
  • lwt36
  • lwt36
  • 2016年02月26日 23:09
  • 194

Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets(DP))

题目链接:点击打开链接 题意:给你一个长度为m的只含()的括号串s, 要求在s两端在加两个串p和q, 使得总长度为n,并且平衡, 平衡是指任意前缀串的(都不少于), 并且整个串的(和)一样多。 思...

Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(简单dp)

题目链接:点这里!!!! 题意: 给你一长度为m仅包含'('和')'的字符串s,叫你组成一个长度为n的字符串。 使得满足下列两个条件: 1、字符串中'('和')'的数量是相等的。 2...

Codeforces Round #422 (Div. 2) E. Liar 贪心 dp 后缀数组

给两个串,a与b,长度1e5,最多可以在a中选出x段互不相交的连续子串,问能不能拼成b,x的范围最多为30。 解法: 可以想到dp,dp[i][j]表示a串前j个字符取 i 段,能匹配到b串的最大前缀...

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)

Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp
举报原因:
原因补充:

(最多只允许输入30个字)