結果

問題 No.1606 Stuffed Animals Keeper
ユーザー 👑 KazunKazun
提出日時 2021-07-16 22:38:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 729 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 77,528 KB
最終ジャッジ日時 2023-09-20 14:58:54
合計ジャッジ時間 7,543 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,368 KB
testcase_01 AC 75 ms
71,196 KB
testcase_02 AC 73 ms
71,380 KB
testcase_03 AC 122 ms
76,924 KB
testcase_04 AC 123 ms
76,812 KB
testcase_05 AC 101 ms
77,040 KB
testcase_06 AC 105 ms
76,780 KB
testcase_07 AC 117 ms
77,444 KB
testcase_08 AC 114 ms
77,008 KB
testcase_09 AC 102 ms
76,840 KB
testcase_10 AC 96 ms
76,864 KB
testcase_11 AC 92 ms
76,620 KB
testcase_12 AC 105 ms
76,880 KB
testcase_13 AC 78 ms
76,448 KB
testcase_14 AC 79 ms
76,608 KB
testcase_15 AC 83 ms
76,484 KB
testcase_16 AC 135 ms
77,432 KB
testcase_17 AC 134 ms
77,496 KB
testcase_18 AC 130 ms
77,180 KB
testcase_19 AC 132 ms
77,528 KB
testcase_20 AC 131 ms
76,972 KB
testcase_21 AC 99 ms
76,748 KB
testcase_22 AC 98 ms
76,876 KB
testcase_23 AC 102 ms
77,344 KB
testcase_24 AC 100 ms
76,768 KB
testcase_25 AC 100 ms
76,828 KB
testcase_26 AC 96 ms
76,756 KB
testcase_27 AC 96 ms
76,708 KB
testcase_28 AC 93 ms
76,708 KB
testcase_29 AC 96 ms
76,892 KB
testcase_30 AC 93 ms
76,872 KB
testcase_31 AC 95 ms
76,708 KB
testcase_32 AC 77 ms
76,636 KB
testcase_33 AC 91 ms
76,736 KB
testcase_34 AC 77 ms
76,164 KB
testcase_35 AC 92 ms
76,700 KB
testcase_36 AC 91 ms
76,692 KB
testcase_37 AC 91 ms
76,748 KB
testcase_38 AC 92 ms
76,696 KB
testcase_39 AC 79 ms
76,488 KB
testcase_40 AC 78 ms
76,492 KB
testcase_41 AC 81 ms
76,652 KB
testcase_42 AC 84 ms
76,556 KB
testcase_43 AC 126 ms
76,688 KB
testcase_44 AC 93 ms
76,548 KB
testcase_45 AC 92 ms
76,588 KB
testcase_46 AC 92 ms
76,604 KB
testcase_47 AC 93 ms
76,572 KB
testcase_48 AC 71 ms
71,064 KB
testcase_49 AC 72 ms
71,100 KB
testcase_50 AC 71 ms
71,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#=================================================
N=int(input())
A=list(map(int,input().split()))+[2]

#===
P=A.count(0)
Q=A.count(1)

#=== 可能どうかの判定
X=[]
x=0
for a in A:
    if a!=2:
        x+=1
    else:
        if x:
            X.append(x)
        x=0

DP=[0]*(P+1); DP[0]=1
for x in X:
    for k in range(P,x-1,-1):
        DP[k]|=DP[k-x]

if DP[P]==0:
    exit(print(-1))

#=== 回数を求める
Y=[]

p=q=0
p_sum=0
for a in A:
    if a==0:
        p+=1
        p_sum+=1
    elif a==1:
        q+=1
    else:
        Y.append((p,q))
        p=q=0

inf=float("inf")
DP=[inf]*(p_sum+1); DP[0]=0

for p,q in Y:
    r=p+q
    for k in range(p_sum,r-1,-1):
        DP[k]=min(DP[k],DP[k-r]+q)

print(DP[p_sum])
0