結果

問題 No.1606 Stuffed Animals Keeper
ユーザー あかしあみどりあかしあみどり
提出日時 2023-03-01 22:21:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,601 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 211,040 KB
最終ジャッジ日時 2023-10-15 01:54:49
合計ジャッジ時間 11,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,548 KB
testcase_01 AC 92 ms
71,540 KB
testcase_02 AC 93 ms
71,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 104 ms
77,344 KB
testcase_14 AC 100 ms
77,120 KB
testcase_15 AC 104 ms
77,420 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 101 ms
77,496 KB
testcase_33 WA -
testcase_34 AC 102 ms
77,376 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 101 ms
77,384 KB
testcase_40 AC 101 ms
77,368 KB
testcase_41 AC 100 ms
77,336 KB
testcase_42 AC 99 ms
77,232 KB
testcase_43 WA -
testcase_44 AC 322 ms
209,256 KB
testcase_45 AC 321 ms
209,532 KB
testcase_46 AC 319 ms
209,508 KB
testcase_47 AC 323 ms
209,880 KB
testcase_48 AC 92 ms
71,728 KB
testcase_49 AC 93 ms
71,476 KB
testcase_50 AC 90 ms
71,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def oi(): return int(input())
def os(): return input()
def mi(): return list(map(int, input().split()))

# import sys
# input = sys.stdin.readline
# import sys
# sys.setrecursionlimit(10**8)
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
input_count = 0

from collections import Counter
N = oi()
A = mi()
elements = []
temp = {0:0, 1:0}
for a in A:
    if a == 2:
        if len(temp)!=0:
            elements.append(temp)
        temp = {0:0, 1:0}
    else:
        temp[a] += 1            
if len(temp)!=0:
    elements.append(temp)
zero_num = 0
one_num = 0
diffs = []
for e in elements:
    if e[0] > e[1]:
        one_num += e[1]
        diffs.append([e[0] + e[1], e[0] - e[1], 0]) #  全体の0と1の, 増える交換回数, 同じかフラグ
    elif e[0] < e[1]:
        zero_num += e[0]
        diffs.append([-1*(e[0] + e[1]), e[1] - e[0], 0])
    else:
        one_num += e[1]
        diffs.append([e[0] + e[1], 0, 1])
if zero_num ==  one_num:
    print(zero_num)
else:
    # 一致してなければDP
    INF = 10**5
    dp = [[INF] * (N*2 +2) for _ in range(len(elements)+1)]

    dp[0][N+1] = 0
    for y,d in enumerate(diffs):
        for x in range(0, 2*N+2):
            if dp[y][x] != INF:
                if 0 <= x+d[0] <= 2*N+1:
                    dp[y+1][x+d[0]] = min(dp[y+1][x+d[0]], dp[y][x]+d[1])
                if d[2] == 1:
                    if 0 <= x-d[0] <= 2*N+1:
                        dp[y+1][x-d[0]] = min(dp[y+1][x-d[0]], dp[y][x]+d[1])

    ret = dp[-1][zero_num-one_num + N+1]
    if ret != INF:
        print(ret)
    else:
        print(-1)
0