結果

問題 No.3114 0→1
ユーザー しとりん
提出日時 2025-04-20 12:48:49
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 678 bytes
コンパイル時間 470 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2025-04-20 12:48:58
合計ジャッジ時間 8,717 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def main():
    N = int(input())
    S = input().strip()

    INF = 10**18
    dp = {(1, 1): 0}

    for c in S:
        ndp = {}
        for (l2, l1), cost in dp.items():
            for out, add in ((0, 0), (1, 1)) if c == '0' else ((1, 0),):
                if l1 == 0 and out == 0:
                    continue
                if l2 == 0 and l1 == 1 and out == 0:
                    continue

                nc = cost + add
                key = (l1, out)
                if nc < ndp.get(key, INF):
                    ndp[key] = nc

        dp = ndp

    ans = min(dp.values())
    print(ans)

if __name__ == "__main__":
    main()
0