結果

問題 No.3114 0→1
ユーザー takanami
提出日時 2025-04-20 04:58:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,015 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2025-04-20 04:58:34
合計ジャッジ時間 4,253 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def min_operations_to_good_string(S):
    N = len(S)
    ones = S.count('1')
    blocks = []
    i = 0

    while i < N:
        if S[i] == '0':
            start = i
            while i < N and S[i] == '0':
                i += 1
            length = i - start
            if length >= 2:
                heapq.heappush(blocks, -length)
        else:
            i += 1

    def count_bad(blocks):
        return sum((l-1)*l//2 for l in blocks)

    block_lengths = [-x for x in blocks]
    bad_substrings = sum((l-1)*l//2 for l in block_lengths)
    ops = 0

    while bad_substrings >= ones and blocks:
        largest = -heapq.heappop(blocks)
        if largest - 1 >= 2:
            heapq.heappush(blocks, -(largest-1))
        block_lengths = [-x for x in blocks]
        bad_substrings = sum((l-1)*l//2 for l in block_lengths)
        ones += 1
        ops += 1

    return ops

if __name__ == "__main__":
    N = int(input())
    S = input().strip()
    print(min_operations_to_good_string(S))
0