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;
#define ll long long
#define ar array
const int mxN=1e5;
int n, q, match[mxN];
bool qry(int l, int r) {
cout << "? " << l+1 << " " << r+1 << endl;
bool res;
cin >> res;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
memset(match, -1, sizeof(match));
string ans(n, '0');
for (int i=1; i<n;) { // right bound of current bracket sequence
if (qry(i-1, i)) { // found
match[i-1]=i, match[i]=i-1;
ans[i-1]='(', ans[i]=')';
int j=i-1;
if (j&&match[j-1]!=-1) {
j=match[j-1];
match[i]=j, match[j]=i;
}
while(j&&i+1<n&&qry(j-1, i+1)) {
++i, --j;
match[i]=j, match[j]=i;
ans[j]='(', ans[i]=')';
if (j&&match[j-1]!=-1) {
j=match[j-1];
match[i]=j, match[j]=i;
}
}
i+=2;
} else
++i;
}
int cnt=count(ans.begin(), ans.end(), '(');
for (int i=0; i<n; ++i)
if (ans[i]=='0') {
if (cnt<n/2)
ans[i]=')', ++cnt;
else
ans[i]='(';
}
cout << "! " << ans << endl;
return 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... |