QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178526#6683. Difficult Constructive ProblemfstqwqAC ✓4ms5988kbC++142.6kb2023-09-14 02:32:202023-09-14 02:32:21

Judging History

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

  • [2023-09-14 02:32:21]
  • 评测
  • 测评结果:AC
  • 用时:4ms
  • 内存:5988kb
  • [2023-09-14 02:32:20]
  • 提交

answer

#include<bits/stdc++.h>
#define N 110000
using namespace std;
int n,k,a[N];char st[N];
int dp[2][N][2];
bool pd(int x,int l,int r){return l<=x && x<=r;}
bool solve(int vst,int ved){
    if(a[1]!=-1 && a[1]!=vst)return 0;
    if(a[n]!=-1 && a[n]!=ved)return 0;
    // printf("OK %d %d %d\n",vst,ved,k);
    if(((2+ved-vst)&1)!=(k&1))return 0;
    a[1]=vst;a[n]=ved;
    dp[0][n][0]=dp[0][n][1]=(ved!=0);
    dp[1][n][0]=dp[1][n][1]=(ved!=1);
    for(int i=n-1;i>=2;i--){
        if(a[i]!=-1){
            dp[0][i][0]=dp[a[i]][i+1][0]+(0!=a[i]);
            dp[0][i][1]=dp[a[i]][i+1][1]+(0!=a[i]);
            dp[1][i][0]=dp[a[i]][i+1][0]+(1!=a[i]);
            dp[1][i][1]=dp[a[i]][i+1][1]+(1!=a[i]);
        }
        else{
            dp[0][i][0]=dp[0][i+1][0];
            dp[0][i][1]=dp[1][i+1][1]+1;
            dp[1][i][0]=dp[1][i+1][0];
            dp[1][i][1]=dp[0][i+1][1]+1;
        }
    }
    // printf("%d %d\n",vst,ved);
    // for(int i=2;i<=n;i++){
    //     printf("%d:%d %d %d %d\n",i,dp[0][i][0],dp[0][i][1],dp[1][i][0],dp[1][i][1]);
    // }
    int minval=dp[a[1]][2][0],maxval=dp[a[1]][2][1];
    if(pd(k,minval,maxval));
    else return 0;
    int res=k,pre=a[1];st[1]=a[1]+'0';
    for(int i=2;i<=n;i++){
        if(a[i]!=-1){
            res-=(a[i]!=pre);
            pre=a[i];
        }
        else{
            int nexres=res-pre;
            if(pd(nexres,dp[0][i+1][0],dp[0][i+1][1]))res=nexres,pre=0;
            else res=res-(pre^1),pre=1;
        }
        st[i]=pre+'0';
    }
    st[n+1]='\0';
    printf("%s\n",st+1);
    assert(res==0);
    return 1;
}
int main(){
    int T;scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        scanf("%s",st+1);
        if(n==1){
            if(st[1]!='1')printf("0\n");
            else printf("1\n");
            continue;
        }
        for(int i=1;i<=n;i++){
            if(st[i]=='?')a[i]=-1;
            else if(st[i]=='0')a[i]=0;
            else a[i]=1;
        }
        if(solve(0,0))continue;
        for(int i=1;i<=n;i++){
            if(st[i]=='?')a[i]=-1;
            else if(st[i]=='0')a[i]=0;
            else a[i]=1;
        }
        if(solve(0,1))continue;
        for(int i=1;i<=n;i++){
            if(st[i]=='?')a[i]=-1;
            else if(st[i]=='0')a[i]=0;
            else a[i]=1;
        }
        if(solve(1,0))continue;
        for(int i=1;i<=n;i++){
            if(st[i]=='?')a[i]=-1;
            else if(st[i]=='0')a[i]=0;
            else a[i]=1;
        }
        if(solve(1,1))continue;
        printf("Impossible\n");
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
9 6
1?010??01
9 5
1?010??01
9 6
100101101
9 5
100101101
9 3
????????1

output:

100100101
Impossible
100101101
Impossible
000000101

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 4ms
memory: 5988kb

input:

6110
2 0
01
9 5
????110??
18 8
???111???00??010??
11 8
001??01???0
2 0
00
8 1
?????111
11 2
110???01???
13 7
??100???01??1
6 2
?????0
18 8
110??11??011??110?
12 5
00??011??00?
20 10
??01???0011???0010??
1 0
?
13 6
???10??110??0
6 2
00???1
17 10
??010??001???11??
5 2
????0
14 7
010???00???11?
2 1
01
...

output:

Impossible
001011001
000111000000101010
00101010010
00
00000111
11000001111
0010000101001
000010
110001100011001101
000001101001
00010000011001001010
0
0001000110010
Impossible
00010010010101100
00010
01000100010111
01
0100100001
Impossible
001100010100100101
00111111000
000
01111001
001000000100010...

result:

ok 6110 lines