zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    gradle平级项目引用
    java使用ssh访问Linux的项目jscraft
    debian更新源时找不到公钥的解决办法
    debian系在线安装软件apt-get命令族
    vim打造开发IDE
    Mysql主从同步配置
    byte[] 转Hex String
    记录一次条件比较多的SQL查询语句
    LruCache的缓存策略
    LinkedHashMap的实现原理
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4256239.html
Copyright © 2011-2022 走看看