#include <iostream>
#include <string.h>
#include <random>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <valarray>
#include <memory>
#include <cassert>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<int, short> pish;
typedef pair<string, string> pss;
typedef pair<int, char> pic;
typedef pair<pii, int> piii;
typedef pair<double, double> pdd;
typedef pair<float, float> pff;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef pair<uint, uint> puint;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> plll;
typedef pair<pll, ld> plld;
typedef pair<int, ll> pil;
typedef pair<ull, ull> pull;
typedef pair<ld, ld> pld;
typedef complex<double> cd;
//#define max(n, m) ((n>m)?n:m)
//#define min(n, m) ((n<m)?n:m)
#define f first
#define s second
#define input() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define eps 1e-9
#define leni(x) sizeof(x)/sizeof(int)
//#define cin fin
//#define cout fout
const int mod = 1000000007;
int msum(int a, int b)
{
a += b;
if(a >= mod) a -= mod;
return a;
}
int nums[1000010];
int pre;
char col[1000010];
int dp[310][310][310];
queue<int> cur;
int main()
{
input();
int p, t, n;
string st;
cin >> p >> t;
if(p == 1)
{
while(t--)
{
cin >> st;
n = (int)st.length();
for(int i = 0; i < n; i ++)
nums[i] = (st[i]=='(')?1:-1;
pre = 0;
//if prefix becomes < 0, remove two ')' from the beginning
//if prefix ends up positive in the end, remove '(' from the end
bool bad = false;
while(cur.size())
cur.pop();
for(int i = 0; i < n; i ++)
col[i] = 'G';
for(int i = 0; i < n; i ++)
{
if(nums[i] == -1)
{
cur.push(i);
pre --;
if(pre == -1)
{
if(cur.size() < 2) { bad = true; break; }
int a = cur.front(); cur.pop(); int b = cur.front(); cur.pop();
col[a] = 'R'; col[b] = 'B';
pre = 0;
}
}
else pre ++;
}
if(nums[n-1] == 1 || bad)
{
cout << "impossible\n";
continue;
}
if(pre > 0)
{
int num = pre*2;
char c = 'R';
for(int i = n-1; i >= 0; i --)
{
if(num == 0) break;
if(nums[i] == 1)
{
col[i] = c;
c = ((c=='R')?'B':'R');
num --;
}
}
if(num)
{
cout << "impossible\n";
continue;
}
}
pre = 0;
for(int i = 0; i < n; i ++)
{
pre += nums[i]*((col[i]=='G')?2:1);
if(pre < 0)
{
bad = true;
break;
}
}
if(pre > 0 || bad)
{
cout << "impossible\n";
continue;
}
for(int i = 0; i < n; i ++)
cout << col[i];
cout << '\n';
}
}
else
{
int lim = 310; //the limit for lower and upper bounds
dp[0][0][0] = 1;
for(int i = 0; i < 300; i ++)
for(int j = 0; j < lim; j ++)
for(int k = 0; k < lim; k ++)
{
dp[i+1][j+1][k+2] = msum(dp[i+1][j+1][k+2], dp[i][j][k]);
if(k > 0) //we can do max(0, j-2) because we can set one ) to R and the other to B
dp[i+1][max(0, j-2)][k-1] = msum(dp[i+1][max(0, j-2)][k-1], dp[i][j][k]);
}
while(t--)
{
cin >> n;
int ans = 0;
for(int i = 0; i < lim; i ++)
ans = msum(ans, dp[n][0][i]);
cout << ans << '\n';
}
}
}
Compilation message
parentrises.cpp: In function 'int main()':
parentrises.cpp:164:39: warning: iteration 308 invokes undefined behavior [-Waggressive-loop-optimizations]
164 | dp[i+1][j+1][k+2] = msum(dp[i+1][j+1][k+2], dp[i][j][k]);
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parentrises.cpp:162:34: note: within this loop
162 | for(int k = 0; k < lim; k ++)
| ~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
17 ms |
988 KB |
Output is correct |
22 |
Correct |
32 ms |
9188 KB |
Output is correct |
23 |
Correct |
25 ms |
1748 KB |
Output is correct |
24 |
Correct |
32 ms |
5112 KB |
Output is correct |
25 |
Correct |
38 ms |
9192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
218 ms |
246024 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
218 ms |
246024 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
218 ms |
246024 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |