# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
672398 | horiseun | Zagrade (COI20_zagrade) | C++11 | 0 ms | 0 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 "cia.h"
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
stack<int> unpaired;
char s[100005];
int count;
string solve(int n, int q) {
unpaired.push(0);
for (int i = 1; i < n; i++) {
if (unpaired.empty()) {
unpaired.push(i);
continue;
}
if (query(unpaired.top(), i)) {
s[unpaired.top()] = '(';
s[i] = ')';
unpaired.pop();
} else {
unpaired.push(i);
}
}
for (int i = 0; i < n; i++) {
if (!(s[i] == ')' || s[i] == '(')) count++;
}
count /= 2;
string ans = "";
for (int i = 0; i < n; i++) {
if (s[i] == ')' || s[i] == '(') {
ans += s[i];
} else {
if (count) {
ans += ')';
count--;
} else {
ans += '(';
}
}
}
return ans;
}