답안 #1082547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082547 2024-08-31T15:13:37 Z serifefedartar Superpozicija (COCI22_superpozicija) C++17
10 / 110
32 ms 16440 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 {

                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();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 32 ms 13660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 13692 KB Output is correct
2 Correct 14 ms 14300 KB Output is correct
3 Correct 14 ms 14464 KB Output is correct
4 Correct 15 ms 14788 KB Output is correct
5 Correct 14 ms 14928 KB Output is correct
6 Correct 10 ms 14968 KB Output is correct
7 Correct 14 ms 15192 KB Output is correct
8 Correct 13 ms 15708 KB Output is correct
9 Correct 15 ms 16124 KB Output is correct
10 Correct 15 ms 16440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 12888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 32 ms 13660 KB Output isn't correct
2 Halted 0 ms 0 KB -