QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#467340#1289. A + B ProblemhmyaAC ✓21ms12100kbC++981.9kb2024-07-08 15:52:142024-07-08 15:52:16

Judging History

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

  • [2024-07-08 15:52:16]
  • 评测
  • 测评结果:AC
  • 用时:21ms
  • 内存:12100kb
  • [2024-07-08 15:52:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int T;
char S[2000005];
int cnt[2000005];
int pre[2000005];
int main(){
    // freopen("partition.in","r",stdin);
    // freopen("partition.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        if(n<m)swap(n,m);
        for(int i=1;i<=n+m;i++)cnt[i]=0;
        getchar();
        scanf("%s",S);
        for(int i=0;i<n+m;i++)pre[i]=(S[i]=='1');
        for(int i=1;i<n+m;i++)pre[i]+=pre[i-1];
        int tot1=n,tot2=m;
        // printf("%s\n",S);
        int st=0;
        int need=n+1,lst=-1;
        int R=1;
        for(int i=0;i<m;i++){
            if(S[i]=='1'){
                int need=n-m+i+1;
                while(R<n+m&&pre[R]-pre[i]<need)R++;
                if(R==n+m)break;
                if(i+1+(R-i-(pre[R]-pre[i]))>m)break;
                tot1=n-need;
                tot2=m-(i+1+((R-i)-(pre[R]-pre[i])));
                st=R+1;
                lst=i;
            }
        }
        for(int i=n;i>tot1;i--)cnt[i]++;
        for(int i=0;i<=lst;i++)cnt[m-i]+=(S[i]=='1');
        for(int i=st;i<n+m;i++){
            if(S[i]=='0'){
                if(tot1<tot2&&tot1)tot1--;
                else if(tot2)tot2--;
                else tot1--;
            }
            else{
                if(tot1<tot2)cnt[tot2--]++;
                else cnt[tot1--]++;
            }
        }
        for(int i=1;i<=n+m;i++){
            cnt[i+1]+=(cnt[i]>1);
            cnt[i]&=1;
        }
        bool tag=false;
        for(int i=n+m;i>=1;i--){
            if(!cnt[i]){
                if(!tag)continue;
                putchar('0');
            }
            else{
                putchar('1');
                tag=true;
            }
        }
        if(!tag){
            putchar('0');
        }
        puts("");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
4 3
1000101
2 2
1111
1 1
00

output:

1101
110
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 21ms
memory: 12100kb

input:

11110
10 8
111011010011100100
3 5
01011000
7 6
1110101010000
9 1
0110100101
1 9
0100001110
8 10
000101101011111000
9 6
011111111000111
1 9
1011101101
10 7
00100011000100000
4 9
1000101101010
8 4
100100110000
8 9
00101111011000101
8 9
11000000101011110
7 6
1111010100110
2 9
01001110101
4 5
100010100
...

output:

10011010100
11100
10101000
110100101
100001110
10000001100
1000010111
111101101
1110100000
111101010
11110000
1000011101
1001011110
10101110
101110101
11100
1111010
1000010
1011100010
10010101001
10010001
1001010
1000000010
1110
111
1111110001
10110111
1100010101
10000000
111000011
110
11111
1100101...

result:

ok 11110 lines