Submission #60638

#TimeUsernameProblemLanguageResultExecution timeMemory
60638egorlifarparentrises (BOI18_parentrises)C++17
100 / 100
103 ms15292 KiB
/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
    
     
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } 
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228                                                         
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
const string FILENAME = "input";
const int Mod = 1000000007;


int sum(int a, int b) {
    return (a + b >= Mod ? a + b - Mod: a + b);
}


void add(int &a, const int &b) {
    a += b;
    if (a >= Mod) {
        a -= Mod;
    }
}


enum Color {
    GREEN = 'G',
    RED = 'R',
    BLUE = 'B',
};
 
template<class F>
string filter(const string& s, const F& pred) {
    string ret;
    for (int i = 0; i < (int) s.length(); ++i) {
        if (pred(i, s[i])) {
            ret += s[i];
        }
    }
    return ret;
}

bool check(string s) {
    int bal = 0;
    for (auto x: s) {
        if (x == '(') {
            bal++;
        } else {
            bal--;
            if (bal < 0) {
                return false;
            }
        }
    }
    return bal == 0;
}


string build(string s) {
    int n = sz(s);
    vector<Color> color(n, Color::GREEN);
    vector<int> greenOpen, greenClosed;
    int balance = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '(') {
            greenOpen.pb(i);
            balance++;
        } else {
            balance--;
            greenClosed.pb(i);
            if (balance < 0) {
                balance = 0;
                if (sz(greenClosed) < 2) {
                    return "impossible";
                }
                int pos1 = greenClosed.back();
                greenClosed.pop_back();
                int pos2 = greenClosed.back();
                greenClosed.pop_back();
                color[pos1] = Color::RED;
                color[pos2] = Color::BLUE;
            }
        }
    }
    while (balance-- > 0) {
        if (sz(greenOpen) < 2) {
            return "impossible";
        }
        int pos1 = greenOpen.back();
        greenOpen.pop_back();
        int pos2 = greenOpen.back();
        greenOpen.pop_back();
        color[pos1] = Color::RED;
        color[pos2] = Color::BLUE;
    }
    string s1 = filter(s, [&color](int index, char c) -> bool {
        return color[index] == Color::RED || color[index] == Color::GREEN;
    });
    string s2 = filter(s, [&color](int index, char c) -> bool {
        return color[index] == Color::BLUE || color[index] == Color::GREEN;
    });
    string ans;
    for (int i = 0; i < n; i++) {
        ans += (char)color[i];
    }
    if (check(s1) && check(s2)) {
        return ans;
    } else {
        return "impossible";
    }
}


vector<int> count(int n) {
    vector<vector<int> > dp(n + 5, vector<int>(2 * n + 5, 0));
    auto ndp = dp;
    vector<int> ans(n + 1);
    ans[0] = 1;
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        for (auto &v: ndp) {
            fill(all(v), 0);
        }
        for (int left = 0; left <= n; left++) {
            for (int right = left; right <= 2 * i; right++) {
                add(ndp[left + 1][right + 2], dp[left][right]);
                int nLeft = max(0, left - 2);
                int nRight = right - 1;
                if (nLeft <= nRight) {
                    add(ndp[nLeft][nRight], dp[left][right]);
                }
            }
        }
        ndp.swap(dp);
        ans[i] = 0;
        for (int right = 0; right <= 2 * i; right++) {
            add(ans[i], dp[0][right]);
        }
    }
    return ans;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//read(FILENAME);
    int p;
    cin >> p;
    if (p == 1) {
        int t;
        cin >> t;
        for (int it = 0; it < t; it++) {
            string s;
            cin >> s;
            cout << build(s) << '\n';
        }
    } else {
        vector<int> q;
        int t;
        cin >> t;
        int need = 0;
        for (int it = 0; it < t; it++) {
            int x;
            cin >> x;
            q.pb(x);
            chkmax(need, x);
        }
        vector<int> ans = count(need);
        for (auto x: q) {
            cout << ans[x] << '\n';
        }
    }
	return 0; 		
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...