QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#235477#3033. Harry Potter and the Palindromic Radiusszaranczuk#100 ✓4980ms520756kbC++174.2kb2023-11-02 20:00:172023-11-02 20:00:17

Judging History

This is the latest submission verdict.

  • [2023-11-02 20:00:17]
  • Judged
  • Verdict: 100
  • Time: 4980ms
  • Memory: 520756kb
  • [2023-11-02 20:00:17]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define st first
#define nd second
using ll = long long;
using pii = pair<int,int>;

const int N = 1e6 + 5;

int n;
int w[N];
int w2[N];
vector<pii> adj[N];
int cnt;
bool vis[N];
int col[N];
int cmp[N];
bool ans;
char ans2[N];
vector<string> ans3;

bool dfs(int v, int c){
    vis[v] = true;
    col[v] = c;
    cmp[v] = cnt;
    for(auto& [u, w] : adj[v]){
        if(vis[u]){
            if(col[u] != col[v] ^ w){
                return false;
            }
        }
        else{
            if(!dfs(u, c^w)){
                return false;
            }
        }
    }
    return true;
}



void fastscan(int &number)
{
    //variable to indicate sign of input number
    bool negative = false;
    register int c;

    number = 0;

    // extract current character from buffer
    c = getchar();
    if (c=='-')
    {
        // number is negative
        negative = true;

        // extract the next character from the buffer
        c = getchar();
    }

    // Keep on extracting characters if they are integers
    // i.e ASCII Value lies from '0'(48) to '9' (57)
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;

    // if scanned input has a negative sign, negate the
    // value of the input number
    if (negative)
        number *= -1;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    int t;
    fastscan(t);
    while(t--) {
        fastscan(n);

        for(int i=0;i<n;i++){
            adj[i].clear();
        }
        fill(w2,w2+n,0);
        ans = true;

        for(int i=0;i<n;i++){
            fastscan(w[i]);
            w[i]++;
        }

        int l=1,r=1;
        w2[0] = 1;
        int op=0;
        bool shit = false;
        for(int i=1;i<n&&!shit;i++){
            w2[i] = max(0, min(r-i, r-i+l<0?0:w2[r-i+l]));
            while(i-w2[i] >= 0 && i+w2[i] < n && w2[i] < w[i]){
                adj[i-w2[i]].emplace_back(i+w2[i], 0);
                adj[i+w2[i]].emplace_back(i-w2[i], 0);
                w2[i]++;
                op++;
                if(op > 4*n){
                    shit = true;
                    break;
                }
            }
            if(i-w2[i] >= 0 && i+w2[i] < n){
                adj[i-w2[i]].emplace_back(i+w2[i], 1);
                adj[i+w2[i]].emplace_back(i-w2[i], 1);
            }
            if(i+w2[i] > r){
                l = i-w2[i], r = i+w2[i];
            }
        }

        if(shit){
            cout << "0\n";
            continue;
        }

        cnt = 0;
        fill(vis,vis+n,0);
        fill(col,col+n,0);
        fill(cmp,cmp+n,0);
        ans = true;
        for(int i=0;i<n;i++){
            if(!vis[i]){
                ans &= dfs(i, 0);
                cnt++;
            }
        }

        if(!ans){
            cout << "0\n";
        }
        else{
            for(int i=0;i<n;i++){
                cmp[i] = cnt-1-cmp[i];
            }

            assert((1<<cnt) < 100);
            ans3.clear();
            for(int m=0;m<(1<<cnt);m++){
                string s;
                for(int i=0;i<n;i++){
                    s += char('0' + ((m>>cmp[i]&1)^(col[i])));
                }
                l = 1, r = 1;
                fill(w2,w2+n,0);
                w2[0] = 1;
                for(int i=1;i<n;i++){
                    //cout << i << " " << l << " " << r << "\n";
                    w2[i] = max(0,min(r-i, r-i+l<0?0:w2[r-i+l]));
                    while(i-w2[i] >= 0 && i+w2[i] < n && s[i-w2[i]] == s[i+w2[i]]) w2[i]++;
                    if(i+w2[i] > r){
                        l = i-w2[i], r = i+w2[i];
                    }
                }
                bool chk = true;
                for(int i=0;i<n;i++){
                    chk &= w[i] == w2[i];
                }
                if(chk){
                    ans3.push_back(s);
                }
            }

            assert(ans3.size() <= 100);
            cout << ans3.size() << "\n";
            for(auto v : ans3){
                cout << v << "\n";
            }
        }
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4980ms
memory: 520756kb

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