QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546543#3223. Cellular AutomatonlefyAC ✓1ms4128kbC++142.5kb2024-09-04 09:00:092024-09-04 09:00:10

Judging History

你现在查看的是最新测评结果

  • [2024-09-04 09:00:10]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:4128kb
  • [2024-09-04 09:00:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=3000;
char a[N];
int n,k,b[N],mask;
struct node{
    int to,nxt,w;
}e[N<<2];
int cnt,head[N];
void add(int u,int v,int w){
    e[++cnt]={v,head[u],w};
    head[u]=cnt;
}
int dis[N],vis[N];
int spfa(){
    queue<int>q;q.push(0);dis[0]=0;
    for(int i=1;i<=mask;i++)dis[i]=1e9,vis[i]=0;
    while(!q.empty()){
        int x=q.front();q.pop();vis[x]=0;
        if(dis[0]<0)break;
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].to,w=e[i].w;
            if(dis[v]>dis[x]+w){
                dis[v]=dis[x]+w;
                if(!vis[v])q.push(v),vis[v]=1;
            }
        }
    }
    // cout<<dis[0]<<"\n";
    return dis[0]==0;
}
int check(){
    for(int i=0;i<=mask;i++)head[i]=0;
    cnt=0;
    for(int s=0;s<=mask;s++){
        if(b[s<<1]==0)add(s,(s<<1)&mask,0),add((s<<1)&mask,s,0);
        if(b[s<<1]==1)add(s,(s<<1)&mask,1),add((s<<1)&mask,s,-1);
        if(b[s<<1]==-1)add(s,(s<<1)&mask,1),add((s<<1)&mask,s,0);
        if(b[s<<1|1]==0)add(s,(s<<1|1)&mask,-1),add((s<<1|1)&mask,s,1);
        if(b[s<<1|1]==1)add(s,(s<<1|1)&mask,0),add((s<<1|1)&mask,s,0);
        if(b[s<<1|1]==-1)add(s,(s<<1|1)&mask,0),add((s<<1|1)&mask,s,1);
    }
    // cout<<dis[0]<<"\n";
    return spfa();
}
void sol(int i){
    b[i]++;for(int j=i+1;j<n;j++)b[j]=-1;
    if(!check()){
        printf("no\n");
        return;
    }
    for(int j=i+1;j<n;j++){
        b[j]=0;
        if(!check())b[j]=1;
    }
    
    for(int j=0;j<n;j++){
        printf("%d",b[j]);
    }
    printf("\n");
}
void solve(){
    scanf("%d%s",&k,a);mask=(1<<2*k)-1;
    n=strlen(a);
    for(int i=0;i<n;i++)b[i]=a[i]-'0';
    if(check()){
        printf("%s\n",a);
        return;
    }
    for(int i=0;i<n;i++)b[i]=-1;
    int las=-1;
    for(int i=0;i<n;i++){
        b[i]=a[i]-'0';
        if(a[i]=='1'){
            if(!check()){
                // cout<<las<<"\n";
                if(las==-1)break;
                else{
                    sol(las);
                    return;
                }
            }
            continue;
        }
        b[i]=1;
        if(check())las=i;
        b[i]=0;
        if(check())continue;
        sol(i);
        return;
    }
    // if(check()){
    //     printf("%s\n",a);
    //     return;
    // }
    printf("no\n");
}
int main() {
    // freopen("ex_string5.in","r",stdin);
    // freopen("1.out","w",stdout);
    int t=1;
    // scanf("%d",&t);
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4056kb

input:

1
11111111

output:

no

result:

ok single line: 'no'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

1
11111101

output:

no

result:

ok single line: 'no'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

1
11001011

output:

no

result:

ok single line: 'no'

Test #4:

score: 0
Accepted
time: 0ms
memory: 4056kb

input:

1
10111110

output:

no

result:

ok single line: 'no'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3764kb

input:

1
10000010

output:

no

result:

ok single line: 'no'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

1
00100011

output:

00110011

result:

ok single line: '00110011'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

1
01000001

output:

01000111

result:

ok single line: '01000111'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

1
00000100

output:

00001111

result:

ok single line: '00001111'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

1
01001000

output:

01010101

result:

ok single line: '01010101'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

1
00001000

output:

00001111

result:

ok single line: '00001111'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

1
00000000

output:

00001111

result:

ok single line: '00001111'

Test #12:

score: 0
Accepted
time: 0ms
memory: 4064kb

input:

2
11111111111111111111111111111111

output:

no

result:

ok single line: 'no'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

2
11111111001111111111111111111110

output:

no

result:

ok single line: 'no'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3872kb

input:

2
11111011011111001110111011011101

output:

no

result:

ok single line: 'no'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

2
11011101110111101111111101110111

output:

no

result:

ok single line: 'no'

Test #16:

score: 0
Accepted
time: 0ms
memory: 4032kb

input:

2
11011011111000101011001101110011

output:

no

result:

ok single line: 'no'

Test #17:

score: 0
Accepted
time: 0ms
memory: 4124kb

input:

2
00010000010001100111101111101110

output:

00010000010100001101111101011111

result:

ok single line: '00010000010100001101111101011111'

Test #18:

score: 0
Accepted
time: 0ms
memory: 3892kb

input:

2
01001010101010011010011000111001

output:

01010000010100000101111101011111

result:

ok single line: '01010000010100000101111101011111'

Test #19:

score: 0
Accepted
time: 0ms
memory: 4064kb

input:

2
10001110000000000000010000100001

output:

no

result:

ok single line: 'no'

Test #20:

score: 0
Accepted
time: 0ms
memory: 3896kb

input:

2
00100010010010010000001000000101

output:

00100011000000000010111111111111

result:

ok single line: '00100011000000000010111111111111'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

2
00100000000001000000100000000000

output:

00100000000011110010110011111111

result:

ok single line: '00100000000011110010110011111111'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3916kb

input:

2
00000000000000000000000000000000

output:

00000000000000001111111111111111

result:

ok single line: '00000000000000001111111111111111'

Test #23:

score: 0
Accepted
time: 0ms
memory: 3804kb

input:

3
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

output:

no

result:

ok single line: 'no'

Test #24:

score: 0
Accepted
time: 0ms
memory: 3780kb

input:

3
10101011111111111111111111111111110111111111110011111111111111101111111110111101111111111111111100111111101111101011111110111111

output:

no

result:

ok single line: 'no'

Test #25:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

3
11111100111111101101111111111110111110111111111110101101010111111111111011011111111101110111011101111111010011111111111011011111

output:

no

result:

ok single line: 'no'

Test #26:

score: 0
Accepted
time: 0ms
memory: 3776kb

input:

3
10111110011110010111101011101011111000100110111111001111111011110110010111111001111101010010110000101111111010111111111111011001

output:

no

result:

ok single line: 'no'

Test #27:

score: 0
Accepted
time: 0ms
memory: 4040kb

input:

3
11101110111001011110110100001111010111000000010010011001011100111111110011100010011010011011111111010011111110000111010111011100

output:

no

result:

ok single line: 'no'

Test #28:

score: 0
Accepted
time: 0ms
memory: 3776kb

input:

3
10101111000011010011010111111011000100100001101000001011111011101000110101011001110110011010001101111010011101111000011111111101

output:

no

result:

ok single line: 'no'

Test #29:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

3
00000000000010000000110001000001011101000011001001000001011101000011010000000001000101000000100101100001010011111100001111100000

output:

00000000000010000000110100000000000000100000100000001111111111111111111111111011000011011111000000000010111110111111111111111111

result:

ok single line: '000000000000100000001101000000...0000010111110111111111111111111'

Test #30:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

3
01000000110100111100001000001001000011100000010101110100001000000010010000110100000001100110001010010000001100000000000001011100

output:

01000001000000000000000000000000000100010000001101010001000000000111110111111111111111111111111111011101111111110101000111111111

result:

ok single line: '010000010000000000000000000000...1011101111111110101000111111111'

Test #31:

score: 0
Accepted
time: 1ms
memory: 3840kb

input:

3
00100010001000100000000000000001100000100000000100000000001010100100000000001000110000000000000000101000000000000000010000000000

output:

00100010001000100000000000000011000001000000000000101100011011110010111011101110000011001111111111110111111111111110111101101111

result:

ok single line: '001000100010001000000000000000...1110111111111111110111101101111'

Test #32:

score: 0
Accepted
time: 1ms
memory: 3828kb

input:

3
00100000000100000010000000010101000000000100000000001100000000000000000000000100100000000001000000000000000000010000010000001000

output:

00100000000100000010000011111111000000000101000000101111011111110010110000010000111011110000000011111111010111111110111101111111

result:

ok single line: '001000000001000000100000111111...1111111010111111110111101111111'

Test #33:

score: 0
Accepted
time: 1ms
memory: 3868kb

input:

3
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

output:

00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111

result:

ok single line: '000000000000000000000000000000...1111111111111111111111111111111'

Test #34:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

1
00011101

output:

00011101

result:

ok single line: '00011101'

Test #35:

score: 0
Accepted
time: 0ms
memory: 4128kb

input:

1
00011000

output:

00011101

result:

ok single line: '00011101'

Test #36:

score: 0
Accepted
time: 0ms
memory: 3756kb

input:

1
11111111

output:

no

result:

ok single line: 'no'