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;
int main() {
int n, q;
cin >> n >> q;
function<bool (int, int)> query = [](int l, int r) {
cout << "? " << l + 1 << " " << r + 1 << endl;
int x;
cin >> x;
return x;
};
string s(n, '0');
stack<pair<int, int>> inters; int cnt = 0;
for (int i = 0; i < n-1; ++i) {
if (query(i, i + 1)) {
s[i] = '('; s[i+1] = ')'; ++cnt;
int l = i, r = i + 1;
while (true) {
if (!inters.empty()) {
auto [l1, r1] = inters.top();
if (r1 == l - 1) { l = l1; inters.pop(); }
}
if (l > 0 && r < n - 1 && query(l - 1, r + 1)) {
--l; ++r;
s[l] = '('; s[r] = ')'; ++cnt;
}
else break;
}
inters.emplace(l, r);
i = r;
}
}
int falta = n/2 - cnt;
for (char &c: s)
if (c == '0')
if (falta > 0) { c = ')'; -- falta; }
else c = '(';
cout << "! " << s << "\n";
}
Compilation message (stderr)
zagrade.cpp: In function 'int main()':
zagrade.cpp:48:6: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
48 | if (c == '0')
| ^
# | 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... |