結果

問題 No.1606 Stuffed Animals Keeper
ユーザー mkawa2mkawa2
提出日時 2021-07-17 10:20:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 612 ms / 3,000 ms
コード長 1,146 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 10,996 KB
実行使用メモリ 8,648 KB
最終ジャッジ日時 2023-09-21 03:16:09
合計ジャッジ時間 11,541 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,084 KB
testcase_01 AC 15 ms
8,108 KB
testcase_02 AC 16 ms
8,104 KB
testcase_03 AC 365 ms
8,152 KB
testcase_04 AC 508 ms
8,380 KB
testcase_05 AC 86 ms
8,012 KB
testcase_06 AC 167 ms
8,036 KB
testcase_07 AC 344 ms
8,056 KB
testcase_08 AC 290 ms
8,184 KB
testcase_09 AC 126 ms
8,092 KB
testcase_10 AC 74 ms
8,192 KB
testcase_11 AC 41 ms
8,032 KB
testcase_12 AC 158 ms
8,184 KB
testcase_13 AC 18 ms
8,408 KB
testcase_14 AC 18 ms
8,372 KB
testcase_15 AC 18 ms
8,504 KB
testcase_16 AC 591 ms
8,484 KB
testcase_17 AC 576 ms
8,508 KB
testcase_18 AC 610 ms
8,508 KB
testcase_19 AC 582 ms
8,508 KB
testcase_20 AC 612 ms
8,564 KB
testcase_21 AC 144 ms
8,164 KB
testcase_22 AC 136 ms
8,408 KB
testcase_23 AC 133 ms
8,596 KB
testcase_24 AC 146 ms
8,408 KB
testcase_25 AC 138 ms
8,428 KB
testcase_26 AC 50 ms
8,536 KB
testcase_27 AC 54 ms
8,388 KB
testcase_28 AC 50 ms
8,412 KB
testcase_29 AC 61 ms
8,464 KB
testcase_30 AC 46 ms
8,412 KB
testcase_31 AC 39 ms
8,416 KB
testcase_32 AC 20 ms
8,484 KB
testcase_33 AC 34 ms
8,480 KB
testcase_34 AC 20 ms
8,388 KB
testcase_35 AC 33 ms
8,416 KB
testcase_36 AC 32 ms
8,468 KB
testcase_37 AC 30 ms
8,484 KB
testcase_38 AC 27 ms
8,492 KB
testcase_39 AC 19 ms
8,424 KB
testcase_40 AC 20 ms
8,484 KB
testcase_41 AC 18 ms
8,476 KB
testcase_42 AC 18 ms
8,356 KB
testcase_43 AC 608 ms
8,564 KB
testcase_44 AC 593 ms
8,464 KB
testcase_45 AC 608 ms
8,648 KB
testcase_46 AC 587 ms
8,460 KB
testcase_47 AC 606 ms
8,256 KB
testcase_48 AC 16 ms
8,036 KB
testcase_49 AC 16 ms
8,192 KB
testcase_50 AC 16 ms
8,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
md = 998244353
# md = 10**9+7

n = II()
aa = LI()+[2]
c0 = c1 = 0
s = 0
seg = []

for i, a in enumerate(aa):
    if a == 0:
        c0 += 1
        s += 1
    elif a == 1:
        c1 += 1
    elif c0+c1:
        seg.append((c0, c1))
        c0 = c1 = 0
# print(seg)

def chmin(j, val):
    if val < dp[j]: dp[j] = val

m = len(seg)
dp = [inf]*(s+1)
dp[0] = 0
for i, (c0, c1) in enumerate(seg):
    for j in range(s-c0-c1+1)[::-1]:
        if dp[j] == inf: continue
        chmin(j+c0+c1, dp[j]+c1)
    # print(dp)

ans = dp[-1]
if ans == inf: ans = -1
print(ans)
0