This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
vector<vector<int>> g;
string solve(string s) {
g.assign(s.size(), {});
vector<int> z[2];
auto check = [&](string s) {
int ok = 1, a = 0, b = 0, c = count(all(s), '('), d = count(all(s), ')');
for(auto i : s) {
ok &= b <= 2*a;
ok &= c <= 2*d;
if(i == ')') swap(a, b), swap(c, d);
a++, c--;
if(i == ')') swap(a, b), swap(c, d);
ok &= b <= 2*a;
ok &= c <= 2*d;
}
return ok;
};
if(!check(s)) return "impossible";
for(auto &i : s) {
int pos = &i-&s[0];
if(i == '(') {
z[0].push_back(pos);
} else {
if(z[0].empty()) {
if(z[1].empty()) return "wimpossible";
g[pos].push_back(z[1].back());
g[z[1].back()].push_back(pos);
z[1].pop_back();
} else {
g[pos].push_back(z[0].back());
g[z[0].back()].push_back(pos);
z[1].push_back(z[0].back());
z[0].pop_back();
}
}/*
cout << pos << ": ";
for(auto i : z[0]) cout << i << " "; cout << " | ";
for(auto i : z[1]) cout << i << " "; cout << endl;*/
}
reverse(all(s));
z[0].clear();
for(auto &i : s) {
int pos = &i-&s[0];
pos = s.size()-1-pos;
if(i == ')') {
if(g[pos].size() < 2) z[0].push_back(pos);
} else if(g[pos].empty()) {
if(z[0].empty()) return "wimpossible";
g[pos].push_back(z[0].back());
g[z[0].back()].push_back(pos);
z[0].pop_back();
}
}
reverse(all(s));
vector<int> col(s.size());
for(int i = 0; i < s.size(); i++) {
if(g[i].size() == 2) {
int c = 1;
for(auto v : g[i]) col[v] = c++;
}
}
s = "";
for(auto i : col) {
if(i == 0) s += "G";
if(i == 1) s += "B";
if(i == 2) s += "R";
}
return s;
}
int F[301];
int dp[302][301];
const int mod = 1e9 + 7;
void add(int &a, int b) {
a = a+b>=mod?a+b-mod:a+b;
}
int f(int n) {
if(F[n] != -1) return F[n];
F[n] = 0;
for(int c0 = 0; c0 <= n; c0++) {
int c1 = n-c0;
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 0; i <= n; i++) {
for(int z = 0; z <= i; z++) {
int o = i-z;
dp[i][z] *= (z <= 2*o && (c1-o) <= 2*(c0-z));
//if(dp[i][z]) cout << i << " " << z << " " << dp[i][z] << " " << F[n] << endl;
add(dp[i+1][z], dp[i][z]);
add(dp[i+1][z+1], dp[i][z]);
if(i == n && z == c0) {
add(F[n], dp[i][z]);
//cout << i << " "<< z << " | " << c0 << " " << dp[i][z] << endl;
}
}
}
}
return F[n];
}
void calc() {
for(int i = 0; i < 301; i++) F[i] = -1;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t, x;
string s;
cin >> t;
if(t == 1) {
cin >> t;
while(t--) {
cin >> s;
cout << solve(s) << '\n';
}
} else {
calc();
cin >> t;
while(t--) cin >> x, cout << f(x) << endl;
}
}
Compilation message (stderr)
parentrises.cpp: In function 'std::string solve(std::string)':
parentrises.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |