# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
968363 | Isam | Superpozicija (COCI22_superpozicija) | C++17 | 21 ms | 3928 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<bits/stdc++.h>
using namespace std;
int n, l[100002], r[100002];
string z;
int ans[100002];
int gr[100002];
set<pair<int, int>> st;
signed main(){
ios_base::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
function<void()> test_case = [](){
cin >> n >> z;
st.clear();
z = '.' + z;
for(register int i = 1; i <= n; ++i){
cin >> l[i] >> r[i];
gr[l[i]] = r[i];
gr[r[i]] = l[i];
if(z[l[i]] == '(' && z[r[i]] == '('){
ans[i] = 0;
z[r[i]] = '.';
}else if(z[l[i]] == ')' && z[r[i]] == ')'){
ans[i] = 1;
z[l[i]] = '.';
}else if(z[l[i]] == '(' && z[r[i]] == ')'){
st.insert({l[i], i});
z[l[i]] = '.';
ans[i] = 1;
}else{ // ) (
st.insert({r[i], i});
z[r[i]] = '.';
ans[i] = 0;
}
}
int tot(0);
for(register int i = 1; i <= 2*n; ++i){
if(z[i] ^ '.') tot += (z[i]=='('?1:-1);
while(tot < 0 && !st.empty()){
int pos = st.begin()->first, j = st.begin()->second;
ans[j] ^= 1;
z[pos] = '(';
z[gr[pos]] = '.';
tot += 2;
st.erase(st.begin());
}
}
function<bool(const string)> is_valid = [](const string &s){
int tot(0);
for(register int i = 0; i < (int)s.size(); ++i){
tot += (s[i]=='('?1:-1);
if(tot < 0) return false;
}
return !tot;
};
string tmp = "";
for(register int i = 1; i <= 2*n; ++i){
if(z[i] == '.') continue;
tmp += z[i];
}
if(is_valid(tmp)){
for(register int i = 1; i <= n; ++i) cout << ans[i] << ' ';
}else{
cout << -1;
}
cout << '\n';
};
while(T--) test_case();
return 0;
}
Compilation message (stderr)
# | 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... |