Submission #1082575

# Submission time Handle Problem Language Result Execution time Memory
1082575 2024-08-31T16:12:08 Z serifefedartar Superpozicija (COCI22_superpozicija) C++17
30 / 110
46 ms 26416 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 100;

#define int long long
int x[MAXN], y[MAXN];
vector<int> L[MAXN], R[MAXN];

void solve() {
    int n;
    string S;
    cin >> n >> S;
    S = "#" + S;

    map<pair<int,int>,int> mp;
    vector<int> ans(n+1, 0);
    vector<int> active(2*n+1, 0);
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
        if (S[x[i]] != S[y[i]]) {
            mp[{x[i], y[i]}] = i;
            mp[{y[i], x[i]}] = i;
            R[y[i]].push_back(x[i]);
            L[x[i]].push_back(y[i]);
        }

        if (S[x[i]] == '(' && S[y[i]] == '(') {
            ans[i] = 0;
            active[x[i]] = 1;
        } else if (S[x[i]] == ')' && S[y[i]] == ')') {
            ans[i] = 1;
            active[y[i]] = 1;
        } else if (S[x[i]] == '(' && S[y[i]] == ')') {
            ans[i] = 1;
            active[y[i]] = 1;
        } else {
            ans[i] = 0;
            active[x[i]] = 1;
        }
    }

    int cnt = 0;
    set<pair<int,int>> completely_left;
    set<pair<int,int>> partially_left;

    for (int i = 1; i <= 2*n; i++) {
        for (auto u : L[i])
            partially_left.insert({u, i}); // {i, u}
        for (auto u : R[i]) {
            if (partially_left.count({i, u}) == 0)
                continue;
            completely_left.insert({u, i});
            partially_left.erase({i, u}); // {u, i}
        }

        if (active[i]) {
            if (S[i] == '(')
                cnt++;
            else
                cnt--;
        }

        if (cnt < 0) {
            if (completely_left.size()) {

                int A = mp[*(completely_left.begin())];
                swap(active[x[A]], active[y[A]]);
                ans[A] ^= 1;
                completely_left.erase(completely_left.begin());
                cnt+=2;

            } else if (partially_left.size()) {

                int A = mp[*(partially_left.begin())];
                swap(active[x[A]], active[y[A]]);
                ans[A] ^= 1;
                partially_left.erase(partially_left.begin());
                cnt++;

            } else {

                for (int i = 1; i <= n; i++) {
                    R[y[i]].clear();
                    L[x[i]].clear();
                }


                cout << "-1\n";
                return ;

            }
        }
    }

    for (int i = 1; i <= n; i++) {
        R[y[i]].clear();
        L[x[i]].clear();
    }

    if (cnt) {
        cout << "-1\n";
        return ;
    }

    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
}

signed main() {
    fast;
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 12892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12892 KB Output is correct
2 Correct 16 ms 13248 KB Output is correct
3 Correct 12 ms 13440 KB Output is correct
4 Correct 14 ms 13736 KB Output is correct
5 Correct 13 ms 13972 KB Output is correct
6 Correct 9 ms 14168 KB Output is correct
7 Correct 11 ms 14424 KB Output is correct
8 Correct 13 ms 14684 KB Output is correct
9 Correct 19 ms 15172 KB Output is correct
10 Correct 19 ms 15160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 26 ms 13916 KB Output is correct
3 Correct 41 ms 15780 KB Output is correct
4 Correct 30 ms 17216 KB Output is correct
5 Correct 34 ms 18572 KB Output is correct
6 Correct 35 ms 20124 KB Output is correct
7 Correct 19 ms 19352 KB Output is correct
8 Correct 32 ms 21944 KB Output is correct
9 Correct 31 ms 23384 KB Output is correct
10 Correct 41 ms 24904 KB Output is correct
11 Correct 46 ms 26416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 12892 KB Output isn't correct
2 Halted 0 ms 0 KB -