結果

問題 No.1818 6 Operations
ユーザー tamato
提出日時 2022-01-21 22:14:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,016 ms / 2,500 ms
コード長 1,018 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 189,568 KB
最終ジャッジ日時 2024-11-26 00:40:55
合計ジャッジ時間 18,642 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
inf = 10 ** 17


def main():
    import sys
    input = sys.stdin.readline

    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    AA = []
    BB = []
    for a in A:
        for _ in range(a):
            AA.append(0)
        AA.append(1)
    for b in B:
        for _ in range(b):
            BB.append(0)
        BB.append(1)
    AA.pop()
    BB.pop()

    NN = len(AA)
    MM = len(BB)
    dp = [[inf] * (MM+1) for _ in range(NN+1)]
    dp[0][0] = 0
    for i in range(NN + 1):
        for j in range(MM + 1):
            cost = 1
            if i > 0 and j > 0:
                if AA[i-1] == BB[j-1]:
                    cost = 0
            if i > 0 and j > 0:
                dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + cost)
            elif i == 0:
                dp[i][j] = j
            elif j == 0:
                dp[i][j] = i
    print(dp[-1][-1])


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