#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i=(a); i<=(b); ++i)
const int maxn = 1e6 + 5;
typedef pair <int, int> PII;
typedef long long ll;
const int mod = 1e9 + 7;
int dp[2][305][605];
//dp[length][stacksize in RG][stacksize in RB]
int type, n;
string s;
int stos[1000100], DL = 0;
char arr[1000100];
int result[305];
void solveone()
{
cin >> s;
int m = (int)s.size();
DL = 0;
for (int i = 0; i < m; ++i)
{
if (s[i] == '(') stos[++DL] = i, arr[i] = 'R';
else
{
if (DL > 0)
{
arr[stos[DL]] = 'B';
arr[i] = 'B';
DL--;
}
else arr[i] = 'R';
}
}
int wait = 0;
for (int i = 0; i < m; ++i)
if (arr[i] == 'R' && s[i] == '(') ++wait;
else if (arr[i] == 'B' && s[i] == ')' && wait > 0)
{
--wait;
arr[i] = 'G';
}
if (wait > 0)
{
cout << "impossible\n";
return;
}
wait = 0;
for (int i = m - 1; i >= 0; --i)
{
if (arr[i] == 'R' && s[i] == ')') ++wait;
else if (arr[i] == 'B' && s[i] == '(' && wait > 0)
{
--wait;
arr[i] = 'G';
}
}
if (wait > 0)
{
cout << "impossible\n";
return;
}
for (int i = 0; i < m; ++i) cout << arr[i];
cout << "\n";
}
inline void addmod(int &x, int v) { x += v; while (x >= mod) x -= mod; }
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> type;
if (type == 1)
{
cin >> n;
for (int i=0; i<n; ++i) solveone();
}
if (type == 2)
{
int tests;
n = 300;
dp[0][0][0] = 1; //one sequence
for (int i=1; i<=n; ++i)
{
FOR(j, 0, n)
FOR(k, 0, n) dp[i & 1][j][k] = 0;
FOR(j, 0, n)
FOR(k, j, n)
{
//i have two options: either update with ) or with (
if (k - 1 >= 0) addmod(dp[i & 1][max(j - 2, 0)][k - 1], dp[(i & 1) ^ 1][j][k]);
addmod(dp[i & 1][j + 1][k + 2], dp[(i & 1) ^ 1][j][k]);
}
for (int j = 0; j <= 2 * i; ++j) addmod(result[i], dp[i & 1][0][j]);
}
cin >> tests;
while (tests--)
{
cin >> n;
cout << result[n] << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
3 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
520 KB |
Output is correct |
2 |
Correct |
3 ms |
520 KB |
Output is correct |
3 |
Correct |
3 ms |
520 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
520 KB |
Output is correct |
2 |
Correct |
3 ms |
520 KB |
Output is correct |
3 |
Correct |
3 ms |
520 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
572 KB |
Output is correct |
7 |
Correct |
3 ms |
572 KB |
Output is correct |
8 |
Correct |
4 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
2 ms |
572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
520 KB |
Output is correct |
2 |
Correct |
3 ms |
520 KB |
Output is correct |
3 |
Correct |
3 ms |
520 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
572 KB |
Output is correct |
7 |
Correct |
3 ms |
572 KB |
Output is correct |
8 |
Correct |
4 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
2 ms |
572 KB |
Output is correct |
11 |
Correct |
3 ms |
572 KB |
Output is correct |
12 |
Correct |
4 ms |
572 KB |
Output is correct |
13 |
Correct |
3 ms |
572 KB |
Output is correct |
14 |
Correct |
3 ms |
580 KB |
Output is correct |
15 |
Correct |
4 ms |
584 KB |
Output is correct |
16 |
Correct |
9 ms |
596 KB |
Output is correct |
17 |
Correct |
10 ms |
988 KB |
Output is correct |
18 |
Correct |
7 ms |
988 KB |
Output is correct |
19 |
Correct |
9 ms |
988 KB |
Output is correct |
20 |
Correct |
9 ms |
988 KB |
Output is correct |
21 |
Correct |
44 ms |
1364 KB |
Output is correct |
22 |
Correct |
74 ms |
3628 KB |
Output is correct |
23 |
Correct |
41 ms |
3628 KB |
Output is correct |
24 |
Correct |
61 ms |
3628 KB |
Output is correct |
25 |
Correct |
62 ms |
3640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
3640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
3640 KB |
Output is correct |
2 |
Correct |
91 ms |
3640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
3640 KB |
Output is correct |
2 |
Correct |
91 ms |
3640 KB |
Output is correct |
3 |
Incorrect |
111 ms |
3640 KB |
Output isn't correct |