QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#831659#1289. A + B ProblemZnPdCoTL 7ms19516kbC++141.4kb2024-12-25 15:55:462024-12-25 15:55:48

Judging History

This is the latest submission verdict.

  • [2024-12-25 15:55:48]
  • Judged
  • Verdict: TL
  • Time: 7ms
  • Memory: 19516kb
  • [2024-12-25 15:55:46]
  • Submitted

answer

#include <bits/stdc++.h>
void solve();
// struct __{ __() { freopen("partition.in", "r", stdin), freopen("partition.out", "w", stdout); } } ___;
int main() { int T; scanf("%d", &T); while (T--) solve(); }
using namespace std;
#define N 2000010
int n, m, k, ca, cb, a[N], b[N], c[N];
char s[N];
void solve() {
    scanf("%d%d%s", &n, &m, s + 1);
    ca = n, cb = m, k = n + m;
    for(int i = 1; i <= k + 10; i++) a[i] = b[i] = 0;
    for(int i = 1; i <= k; i++) s[i] -= '0';
    for(int i = 1; i <= k; i++) c[i] = c[i - 1] + s[i];
    for(int i = 1; i <= k; i++) {
        bool flag = 0;
        if(!ca) flag = 1; 
        else if(!cb) flag = 0;
        else if(ca <= cb) flag = 0;
        else flag = 1;

        if(flag) swap(a, b), swap(ca, cb);

        if(s[i] == 0) {
            a[ca--] = s[i];
        } else {
            if(c[k - ca] - c[i] >= cb - ca) {
                a[ca--] = s[i];
            } else {
                b[cb--] = s[i];
            }
        }

        if(flag) swap(a, b), swap(ca, cb);
    }
    int len = max({n, m, 1});
    for(int i = 1; i <= len; i++) {
        a[i] += b[i];
        if(a[i] >= 2) {
            a[i + 1] += a[i] / 2;
            a[i] %= 2;
            if(i + 1 > len) len = i + 1;
        }
    }
    while(a[len] == 0 && len > 1) len--;
    reverse(a + 1, a + len + 1);
    for(int i = 1; i <= len; i++) printf("%d", a[i]);
    printf("\n");
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 19516kb

input:

3
4 3
1000101
2 2
1111
1 1
00

output:

1101
110
0

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

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:


result: