• 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
    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
    (())()

    #include<iostream>
    #include<algorithm>
    #include<vector>
    
    using namespace std;
    
    int l[1000], r[1000];
    vector<int> v;
    string s;
    
    int main()
    {
        int n;
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            cin >> l[i] >> r[i];
            s += '(';
            v.push_back(i);
            while(v.size() && l[v.back()] <= 1)
            {
                int x = v.back();
                if(r[x] < 1)
                {
                    cout << "IMPOSSIBLE"  << endl;
                    return 0;
                }
                for(int i = 0; i < v.size(); i++)
                    l[v[i]] -= 2, r[v[i]] -= 2;
                s += ')';
                v.pop_back();
            }
        }
        if(v.size())
            cout << "IMPOSSIBLE"  << endl;
        else
            cout << s << endl;
        return 0;
    }
  • 相关阅读:
    git命令
    深度剖析Apache Dubbo核心技术内幕学习笔记
    MyBatis-Plus简介
    Mysql主从复制原理
    springboot启动原理
    布隆过滤器
    java poi生成的excel发送邮件后无法预览
    Git使用教程:最详细、最傻瓜、最浅显、真正手把手教!
    javacv FFmpeg 视频压缩
    .NetCore之接口缓存
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4256239.html
Copyright © 2020-2023  润新知