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

            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;
}

你可能感兴趣的:(Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp)