QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749584#3033. Harry Potter and the Palindromic Radiusredhood#100 ✓13274ms103432kbC++204.3kb2024-11-15 05:20:062024-11-15 05:20:07

Judging History

This is the latest submission verdict.

  • [2024-11-15 05:20:07]
  • Judged
  • Verdict: 100
  • Time: 13274ms
  • Memory: 103432kb
  • [2024-11-15 05:20:06]
  • Submitted

answer

#include<bits/stdc++.h>

#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pb push_back
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)


using namespace std;


using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;



#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif


struct Dsu {
    vector<int> par, sz;
    Dsu(int n) {
        par.resize(n);
        sz.assign(n, 1);
        iota(all(par), 0);
    }

    int get(int v) {
        return par[v] = (par[v] == v ? v : get(par[v]));
    }

    bool merge(int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) {
            return false;
        }
        if (sz[a] > sz[b]) {
            swap(a, b);
        }
        par[a] = b;
        sz[b] += sz[a];
        return true;
    }
};

void solve(){
    int n;
    cin >> n;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]++;

    }

    for (int i = 0; i < n; i++) {
        if (i - p[i] + 1 < 0 || i + p[i] - 1 >= n) {
            cout << "0\n";
            return;
        }

    }

    Dsu dsu(n);

    int l = 0, r = -1;

    for (int i = 0; i < n; i++) {
        debug("i", i);
        int st = (i > r ? 0 : min(r - i + 1, p[l + r - i]));
        if (st > p[i]) {
            cout << "0\n";
            return;
        }
        while (st < p[i]) {
                debug("CONNNECT ", i - st, i + st);
            dsu.merge(i - st, i + st);
            st++;
        }
        if (i + p[i] - 1 > r) {
            r = i + p[i] - 1;
            l = i - p[i] + 1;
        }
    }

    vector<vector<int>> g(n);
    auto add_edge = [&](int v, int u) {
        v = dsu.get(v);
        u = dsu.get(u);
        g[v].push_back(u);
        g[u].push_back(v);
    };

    for (int i = 0; i < n; i++) {
        if (i - p[i] >= 0 && i + p[i] < n) {
            if (dsu.get(i - p[i]) == dsu.get(i + p[i])) {
                cout << "0\n";
                return;
            }
            add_edge(i - p[i], i + p[i]);
        }
    }

    vector<array<vector<int>, 2>> components;

    vector<int> used(n);
    vector<vector<int>> who(n, vector<int>());
    for (int i = 0; i < n; i++) {
        who[dsu.get(i)].push_back(i);
    }

    debug("who", who);

    bool okay = true;
    for (int i = 0; i < n; i++) {
        if (dsu.get(i) != i) {
            continue;
        }
        if (used[i]) {
            continue;
        }
        array<vector<int>, 2> x;
        function<void(int, int)> dfs = [&](int v, int clr) {
                        debug("dfs", v, clr);
            if (used[v]) {
                if (used[v] != clr) {
                    okay = false;
                }
                return;
            }
            used[v] = clr;

            int id =(clr == 1 ? 0 : 1);
            x[id].insert(x[id].end(), who[v].begin(), who[v].end());
            for (auto &z : g[v]) {
                dfs(z, 3 - clr);
            }
        };

        debug(i);
        dfs(i, 1);
        components.emplace_back(x);
    }

    if (!okay) {
        cout << "0\n";
        return;
    }
    debug("wtf");
    vector<string> solutions;

    for (int mask = 0; mask < (1 << components.size()); mask++) {
        string clr(n, '0');
        for (int i = 0; i < components.size(); i++) {
            int j = 0;
            if ((1 << i) & mask) {
                j = 1;
            }
            for (auto &z : components[i][j]) {
                clr[z] = '1';
            }
        }
        solutions.emplace_back(clr);
    }
    sort(all(solutions));
    cout << solutions.size() << "\n";
    for (auto& z : solutions) {
        cout << z << "\n";
    }
}

int main(){
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);


    int t;
    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
/*
3
4
1 39
2 17
4 5
1 40
3
1 10
1 20
1 30
7
5 4
4 3
3 2
2 1
3 2
4 3
5 4
*/



Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 100
Accepted
time: 13274ms
memory: 103432kb

input:

131112
2
0 0
2
0 1
2
0 0
2
1 0
2
0 0
2
0 1
2
0 0
2
0 1
3
0 1 0
3
0 1 1
3
0 0 0
3
0 1 0
3
0 1 0
3
0 2 0
3
0 0 0
3
1 0 0
3
0 0 0
3
1 0 0
3
0 1 0
3
0 2 0
3
0 0 0
3
0 0 1
3
0 1 0
3
0 2 0
4
0 1 1 0
4
0 1 2 0
4
0 0 1 0
4
0 0 1 1
4
0 1 0 0
4
0 1 0 1
4
0 0 0 0
4
0 0 1 0
4
0 0 1 0
4
1 0 1 0
4
0 1 1 0
4
0 1 2...

output:

4
00
01
10
11
0
4
00
01
10
11
0
4
00
01
10
11
0
4
00
01
10
11
0
4
000
010
101
111
0
4
001
011
100
110
4
000
010
101
111
4
000
010
101
111
0
4
001
011
100
110
0
4
001
011
100
110
0
4
000
010
101
111
0
4
001
011
100
110
0
4
000
010
101
111
0
4
0000
0101
1010
1111
0
4
0010
0111
1000
1101
0
4
0001
0100
...

result:

ok 401208 lines