結果

問題 No.2247 01 ZigZag
ユーザー lam6er
提出日時 2025-03-20 20:40:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,414 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,892 KB
実行使用メモリ 81,924 KB
最終ジャッジ日時 2025-03-20 20:40:26
合計ジャッジ時間 4,331 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m, k = map(int, input().split())

def solve():
    if n == 0 and m == 0:
        return -1
    if k == 0:
        if (n == 0 and m == 0) or (n != 0 and m != 0):
            return -1
        return '0' * n + '1' * m if n != 0 else '1' * m

    min_k = 1 if (n > 0 and m > 0) else 0
    a, b = n, m

    if abs(a - b) <= 1:
        max_k = a + b - 1
    else:
        max_k = 2 * min(a, b)

    if k < min_k or k > max_k:
        return -1

    g = k + 1
    x0 = (g + 1) // 2
    y0 = g // 2
    start0_possible = x0 <= a and y0 <= b

    x1 = g // 2
    y1 = (g + 1) // 2
    start1_possible = y1 <= b and x1 <= a

    if not start0_possible and not start1_possible:
        return -1

    if start0_possible:
        use_start0 = True
    else:
        use_start0 = False

    if use_start0:
        x, y = x0, y0
    else:
        x, y = x1, y1

    zero_groups = []
    one_groups = []

    if x > 0:
        first_zero = a - (x - 1)
        zero_groups.append('0' * first_zero)
        zero_groups.extend(['0'] * (x - 1))

    if y > 0:
        if y == 1:
            one_groups.append('1' * b)
        else:
            first_ones = y - 1
            one_groups.extend(['1'] * first_ones)
            remaining_ones = b - first_ones
            if remaining_ones <= 0:
                return -1
            one_groups.append('1' * remaining_ones)

    res = []
    z_idx = 0
    o_idx = 0
    is_zero_turn = use_start0

    while z_idx < len(zero_groups) or o_idx < len(one_groups):
        if is_zero_turn and z_idx < len(zero_groups):
            res.append(zero_groups[z_idx])
            z_idx += 1
            if o_idx < len(one_groups):
                is_zero_turn = False
        elif not is_zero_turn and o_idx < len(one_groups):
            res.append(one_groups[o_idx])
            o_idx += 1
            if z_idx < len(zero_groups):
                is_zero_turn = True
        else:
            if z_idx < len(zero_groups):
                res.append(zero_groups[z_idx])
                z_idx += 1
            elif o_idx < len(one_groups):
                res.append(one_groups[o_idx])
                o_idx += 1

    s = ''.join(res)

    cnt = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            cnt += 1
    if cnt != k:
        return -1

    if s.count('0') != a or s.count('1') != b:
        return -1

    return s

ans = solve()
print(ans if ans is not None else -1)
0