#include <bits/stdc++.h>
using namespace std;
int n, q;
char arr[100005];
int ask(int l, int r) {
cout << "? " << l << " " << r << endl;
int ret;
cin >> ret;
return ret;
}
int main() {
cin >> n >> q;
int sz = -1;
for(int i =1 ; i <= n;i++) {
arr[i] = '.';
}
vector<pair<int, int> > v;
int l = 1, r = 2;
while(r <= n) {
int ret = ask(l, r);
if (ret) {
arr[l] = '(';
arr[r] = ')';
// if (sz >= 0) {
// cout << v[sz].second << " " << l << endl;
// }
while (sz >= 0 && v[sz].second >= l - 1) {
// cout << v[sz].second << " " << l - 1 << endl;
// cout << v[sz].first << " " << v[sz].second << endl;
l = min(l, v[sz].first);
r = max(r, v[sz].second);
v.pop_back();
sz--;
}
// cout << ":: v nya ::" << endl;
// for(int j = 0; j < v.size(); j++) {
// cout << v[j].first << " " << v[j].second << " ";
// }
// cout << "l r" << endl;
// cout << l << " " << r << endl;
sz++;
v.push_back(make_pair(l, r));
// cout << "::" << v.size() << endl;
if (sz >= 1) {
r++;
l--;
}
else if(l > 1) {
r++;
l--;
}
else {
r+=2;
l = r - 1;
}
}
else {
r++;
l = r - 1;
}
}
int tot = 0;
for(int i = 1; i <= n; i++) {
if (arr[i] == '.') {
tot++;
}
}
tot /= 2;
cout << "! ";
int cnt = 0;
for(int i = 1; i <= n; i++) {
if (arr[i] == '.') {
if (cnt < tot) {
cout << ')';
}
else {
cout << '(';
}
cnt++;
continue;
}
cout << arr[i];
}
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
6 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
10 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
4 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
11 ms |
208 KB |
Output is correct |
3 |
Correct |
11 ms |
288 KB |
Output is correct |
4 |
Correct |
9 ms |
308 KB |
Output is correct |
5 |
Correct |
8 ms |
308 KB |
Output is correct |
6 |
Correct |
9 ms |
304 KB |
Output is correct |
7 |
Correct |
8 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
420 ms |
336 KB |
Output is correct |
3 |
Correct |
886 ms |
336 KB |
Output is correct |
4 |
Correct |
762 ms |
456 KB |
Output is correct |
5 |
Correct |
705 ms |
336 KB |
Output is correct |
6 |
Correct |
852 ms |
376 KB |
Output is correct |
7 |
Correct |
795 ms |
336 KB |
Output is correct |
8 |
Correct |
851 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
660 ms |
380 KB |
Output is correct |
3 |
Correct |
765 ms |
652 KB |
Output is correct |
4 |
Correct |
882 ms |
708 KB |
Output is correct |
5 |
Correct |
841 ms |
492 KB |
Output is correct |
6 |
Correct |
701 ms |
572 KB |
Output is correct |
7 |
Correct |
832 ms |
584 KB |
Output is correct |
8 |
Correct |
882 ms |
380 KB |
Output is correct |