/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#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 time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
572 KB |
Output is correct |
2 |
Correct |
2 ms |
572 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
3 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
572 KB |
Output is correct |
2 |
Correct |
2 ms |
572 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
3 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
572 KB |
Output is correct |
2 |
Correct |
2 ms |
572 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
3 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
732 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
764 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
3 ms |
812 KB |
Output is correct |
16 |
Correct |
8 ms |
812 KB |
Output is correct |
17 |
Correct |
7 ms |
2184 KB |
Output is correct |
18 |
Correct |
7 ms |
2184 KB |
Output is correct |
19 |
Correct |
10 ms |
2184 KB |
Output is correct |
20 |
Correct |
7 ms |
2184 KB |
Output is correct |
21 |
Correct |
59 ms |
2184 KB |
Output is correct |
22 |
Correct |
54 ms |
15276 KB |
Output is correct |
23 |
Correct |
33 ms |
15276 KB |
Output is correct |
24 |
Correct |
41 ms |
15276 KB |
Output is correct |
25 |
Correct |
53 ms |
15292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15292 KB |
Output is correct |
2 |
Correct |
3 ms |
15292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15292 KB |
Output is correct |
2 |
Correct |
3 ms |
15292 KB |
Output is correct |
3 |
Correct |
103 ms |
15292 KB |
Output is correct |