QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142197#6683. Difficult Constructive ProblemBetter_OIerAC ✓16ms3712kbC++143.4kb2023-08-18 16:58:122023-08-18 16:58:14

Judging History

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

  • [2023-08-18 16:58:14]
  • 评测
  • 测评结果:AC
  • 用时:16ms
  • 内存:3712kb
  • [2023-08-18 16:58:12]
  • 提交

answer

/*=================================================
*Le vent se lève,il faut tenter de vivre!
*Author: Better_OIer Zyx
*起风了,唯有努力生存!
*Blog: https://betteroier.site:1000
*Fileansstation: https://betteroier.site:1005
*OnlineJudge: http://betteroier.site:8888
=================================================*/
#include<iostream>
using namespace std;
int n,k,cnt;
char s[200005];
string anss;
void solve(){
    cnt=k;anss="";
    for(int i=1;i<n;i++){
        if(s[i]=='?'||s[i+1]=='?')continue;
        if(s[i]!=s[i+1])cnt--;
    }
    //找出现在已经有了的k并减去
    int minn=0,maxn=0;
    for(int i=1;i<n;i++){
        if(s[i]!='?'&&s[i+1]=='?'){
            int len=0;//都是?的区间长度
            while(i+len+1<=n&&s[i+len+1]=='?')len++;
            if(s[i]!=s[i+len+1]){
                minn++;maxn+=(len/2)*2+1;
            }else maxn+=(len+1)/2*2;
            i+=len;
        }
    }
    if(cnt<minn||cnt>maxn||((cnt&1)!=(minn&1))){
        anss="Impossible";
    }else{
        for(int i=1;i<=n;i++){
            if(s[i]!='?')anss+=s[i];
            else{
                int len=1;
                while(s[i+len]=='?')len++;
                if(s[i-1]!=s[i+len]) minn--,maxn-=(len/2)*2+1;
                else maxn-=(len+1)/2*2;

                int minPI=max(0,cnt-maxn),maxPI=cnt-minn;
                if(s[i-1]!=s[i+len]){
                    minPI--;maxPI--;
                    minPI=(minPI+1)/2;maxPI/=2;
                    if(s[i-1]=='0'){
                        int t=len-(minPI*2);cnt--;
                        while(t--)anss+="0";
                        while(minPI--) anss+="10",cnt-=2;
                    }else{
                        int t=len-(minPI*2);
                        while(t--){anss+="0";}cnt--;//单个补全
                        while(minPI--) anss+="01",cnt-=2;
                    }
                }else if(s[i-1]==s[i+len]){
                    minPI=(minPI+1)/2;maxPI/=2;
                    if(s[i-1]=='0'){
                        int t=len-max(0,(minPI*2)-1);
                        while(t--) anss+="0";
                        if(minPI) anss+="1",minPI--,cnt-=2;
                        while(minPI--) anss+="01",cnt-=2;
                    }else{
                        if(maxPI==0){//不能填了,直接插1
                            int t=len;
                            while(t--)anss+="1";
                        }else{
                            if(minPI)minPI--;
                            int t=len-(minPI*2);
                            while(t--){anss+="0";}cnt-=2;
                            while(minPI--)anss+="10",cnt-=2;
                        }
                    }
                }
                i+=len-1;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n>>k>>(s+1);
        string ans="Impossible";
        bool wh_1 = (s[1] == '?'), wh_2 = (s[n] == '?');
        for(int j=0;j<=1;j++)
            if(wh_1||s[1]=='0'+j){
                s[1]='0'+j;
                for(int w=0;w<=1;w++)
                    if(wh_2||s[n]=='0'+w){
                        s[n]='0'+w;
                        solve();
                        ans=min(ans,anss);
                    }
            }
        cout<<ans<<endl;
    }
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3528kb

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: 16ms
memory: 3712kb

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