結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー |
![]() |
提出日時 | 2021-07-17 10:20:57 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 642 ms / 3,000 ms |
コード長 | 1,146 bytes |
コンパイル時間 | 398 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-07-06 21:55:41 |
合計ジャッジ時間 | 11,138 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
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)