【第22期】观点:IT 行业加班,到底有没有价值?

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Arthur and Brackets - CodeForces 508 E dp贪心

Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes ...

[DP]Codeforces Round #264 (Div. 2) D

大致题意: 求多个数列(n=1000)的最长公共子串。   大致思路: 一开始没有头绪,后来发现一点,长度为n的数列中每个数都属于1--n且不重复,所以根据每个数列中每个数字的位置来判定即可。   #include&lt;iostream&gt; #...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

Codeforces 437E The Child and Polygon(区间DP)

<p style="margin-top: 0px; margin-bottom: 10px; line-h

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

E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes ...

Codeforces Round #104 (Div. 2) E Lucky Subsequence

迟到的ac,不错的题目,要预处理 n!mod 1000,000,007 和 n!对于 mod 1000,000,0007 的逆元,否则第22组数据的时候会超时!算 l乘到r mod p的时候只要算 r!mod p 再乘以 (l-1)! 对 mod p 的逆元,预处理复杂度O(n),查询复杂度O(1)...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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