// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
void solve() {
int N; cin >> N;
string S; cin >> S;
vi L(N), R(N), ans(N);
V<array<int, 2>> A; set<array<int, 2>> F; vi rem; set<int> pos;
for(int i = 0; i < N; i++) {
cin >> L[i] >> R[i]; --L[i], --R[i];
if (S[L[i]] == '(' && S[R[i]] == '(') {
rem.pb(L[i]); ans[i] = 0;
}
if (S[L[i]] == ')' && S[R[i]] == ')') {
pos.insert(R[i]); ans[i] = 1;
}
if (S[L[i]] == '(' && S[R[i]] == ')') {
pos.insert(R[i]);
A.pb({L[i], i});
ans[i] = 1;
}
if (S[L[i]] == ')' && S[R[i]] == '(') {
pos.insert(L[i]);
A.pb({L[i], i});
ans[i] = 0;
}
}
auto pair = [&](int x) {
auto it = pos.lower_bound(x);
if (it == end(pos)) return 0;
pos.erase(it); return 1;
};
for(auto& x : rem) {
if (!pair(x)) {
cout << -1 << nl;
return;
}
}
sort(begin(A), end(A));
int p = -1;
while(sz(pos)) {
int lim = *begin(pos);
while(p + 1 < sz(A) && A[p + 1][0] <= lim) {
++p; int x = A[p][1];
F.insert({R[x], x});
}
bool ok = 1;
if (!sz(F)) ok = 0;
else {
int x = (*begin(F))[1]; F.erase(begin(F));
ans[x] ^= 1;
if (!pair(L[x])) ok = 0;
if (!pair(R[x])) ok = 0;
}
if (!ok) {
cout << -1 << nl;
return;
}
}
// cout << 1 << nl;
for(auto& x : ans) cout << x << " ";
cout << nl;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int T; cin >> T; while(T--) {
solve();
}
exit(0-0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
1112 KB |
Output is correct |
2 |
Correct |
24 ms |
1204 KB |
Output is correct |
3 |
Correct |
28 ms |
1316 KB |
Output is correct |
4 |
Correct |
28 ms |
1116 KB |
Output is correct |
5 |
Correct |
32 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
1368 KB |
Output is correct |
2 |
Correct |
21 ms |
1884 KB |
Output is correct |
3 |
Correct |
20 ms |
2136 KB |
Output is correct |
4 |
Correct |
23 ms |
2828 KB |
Output is correct |
5 |
Correct |
21 ms |
2932 KB |
Output is correct |
6 |
Correct |
13 ms |
2904 KB |
Output is correct |
7 |
Correct |
17 ms |
3468 KB |
Output is correct |
8 |
Correct |
19 ms |
4196 KB |
Output is correct |
9 |
Correct |
23 ms |
4716 KB |
Output is correct |
10 |
Correct |
25 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
22 ms |
1364 KB |
Output is correct |
3 |
Correct |
25 ms |
2072 KB |
Output is correct |
4 |
Correct |
24 ms |
2652 KB |
Output is correct |
5 |
Correct |
31 ms |
3164 KB |
Output is correct |
6 |
Correct |
33 ms |
3916 KB |
Output is correct |
7 |
Correct |
15 ms |
3676 KB |
Output is correct |
8 |
Correct |
24 ms |
4456 KB |
Output is correct |
9 |
Correct |
19 ms |
5244 KB |
Output is correct |
10 |
Correct |
38 ms |
5924 KB |
Output is correct |
11 |
Correct |
36 ms |
6620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
1112 KB |
Output is correct |
2 |
Correct |
24 ms |
1204 KB |
Output is correct |
3 |
Correct |
28 ms |
1316 KB |
Output is correct |
4 |
Correct |
28 ms |
1116 KB |
Output is correct |
5 |
Correct |
32 ms |
1112 KB |
Output is correct |
6 |
Correct |
17 ms |
1368 KB |
Output is correct |
7 |
Correct |
21 ms |
1884 KB |
Output is correct |
8 |
Correct |
20 ms |
2136 KB |
Output is correct |
9 |
Correct |
23 ms |
2828 KB |
Output is correct |
10 |
Correct |
21 ms |
2932 KB |
Output is correct |
11 |
Correct |
13 ms |
2904 KB |
Output is correct |
12 |
Correct |
17 ms |
3468 KB |
Output is correct |
13 |
Correct |
19 ms |
4196 KB |
Output is correct |
14 |
Correct |
23 ms |
4716 KB |
Output is correct |
15 |
Correct |
25 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
22 ms |
1364 KB |
Output is correct |
18 |
Correct |
25 ms |
2072 KB |
Output is correct |
19 |
Correct |
24 ms |
2652 KB |
Output is correct |
20 |
Correct |
31 ms |
3164 KB |
Output is correct |
21 |
Correct |
33 ms |
3916 KB |
Output is correct |
22 |
Correct |
15 ms |
3676 KB |
Output is correct |
23 |
Correct |
24 ms |
4456 KB |
Output is correct |
24 |
Correct |
19 ms |
5244 KB |
Output is correct |
25 |
Correct |
38 ms |
5924 KB |
Output is correct |
26 |
Correct |
36 ms |
6620 KB |
Output is correct |
27 |
Correct |
32 ms |
1368 KB |
Output is correct |
28 |
Correct |
40 ms |
2228 KB |
Output is correct |
29 |
Correct |
46 ms |
2640 KB |
Output is correct |
30 |
Correct |
46 ms |
3296 KB |
Output is correct |
31 |
Correct |
42 ms |
3800 KB |
Output is correct |
32 |
Correct |
27 ms |
3800 KB |
Output is correct |
33 |
Correct |
35 ms |
4552 KB |
Output is correct |
34 |
Correct |
44 ms |
5220 KB |
Output is correct |
35 |
Correct |
47 ms |
5976 KB |
Output is correct |
36 |
Correct |
62 ms |
6592 KB |
Output is correct |