# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
62104 | gs14004 | parentrises (BOI18_parentrises) | C++17 | 1063 ms | 6668 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
using lint = long long;
const int MAXN = 1000050;
bool det(string s){
int qcnt = 0, pcnt = 0;
int bcnt = 0;
int numcnt = 0;
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
qcnt++;
bcnt++;
}
else{
if(bcnt > 0){
bcnt--;
}
else{
if(qcnt == pcnt) return 0;
pcnt++;
}
}
}
bcnt = s.size() - qcnt - bcnt;
if(bcnt < 0) return 0;
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
if(pcnt > 0){
numcnt += 2;
pcnt--;
}
else{
numcnt += 1;
}
}
else{
if(bcnt == 0){
numcnt -= 2;
}
else{
bcnt--;
numcnt -= 1;
}
}
if(numcnt < 0) return 0;
}
return numcnt == 0;
}
int f(int n){
int ans = 0;
for(int i=0; i<(1<<n); i++){
string s;
for(int j=0; j<n; j++){
if((i >> j) & 1) s.push_back('(');
else s.push_back(')');
}
if(det(s)) ans++;
}
return ans;
}
string solve(string s){
queue<int> que;
string chk;
int cnt = 0;
chk.resize(s.size());
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
que.push(i);
cnt++;
chk[i] = '?';
}
else{
if(cnt > 0){
chk[i] = '!';
cnt--;
}
else{
if(que.empty()) return "impossible";
int x = que.front();
que.pop();
chk[x] = 'G';
chk[i] = '!';
}
}
}
for(int i=(int)s.size()-1; i>=0; i--){
if(s[i] == ')' && cnt){
chk[i] = 'G';
cnt--;
}
}
if(cnt > 0) return "impossible";
int c0 = 0, c1 = 0;
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
if(chk[i] == 'G') cnt += 2;
else{
cnt++;
if(cnt & 1) chk[i] = 'R';
else chk[i] = 'B';
}
if(chk[i] != 'B') c0++;
if(chk[i] != 'R') c1++;
}
else{
if(chk[i] == 'G') cnt -= 2;
else{
if(cnt & 1) chk[i] = 'R';
else chk[i] = 'B';
cnt--;
}
if(chk[i] != 'B') c0--;
if(chk[i] != 'R') c1--;
}
if(c0 < 0 || c1 < 0) return "impossible";
}
if(c0 == 0 && c1 == 0) return chk;
else return "impossible";
}
int main(){
char buf[MAXN];
int p, t; cin >> p;
if(p == 1){
scanf("%d",&t);
while(t--){
scanf("%s",buf);
string ans = buf;
printf("%s\n", solve(buf).c_str());
}
}
else{
scanf("%d",&t);
while(t--){
int n; cin >> n;
cout << f(n) << endl;
}
}
}
Compilation message (stderr)
# | 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... |