結果

問題 No.50 おもちゃ箱
ユーザー tobusakanatobusakana
提出日時 2022-10-23 22:30:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 311 ms / 5,000 ms
コード長 1,911 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 85,992 KB
最終ジャッジ日時 2023-09-15 06:59:58
合計ジャッジ時間 7,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
76,824 KB
testcase_01 AC 252 ms
85,576 KB
testcase_02 AC 95 ms
76,744 KB
testcase_03 AC 69 ms
71,464 KB
testcase_04 AC 72 ms
71,452 KB
testcase_05 AC 110 ms
85,292 KB
testcase_06 AC 99 ms
76,420 KB
testcase_07 AC 71 ms
71,224 KB
testcase_08 AC 84 ms
76,440 KB
testcase_09 AC 78 ms
76,148 KB
testcase_10 AC 70 ms
71,188 KB
testcase_11 AC 83 ms
76,764 KB
testcase_12 AC 75 ms
76,212 KB
testcase_13 AC 75 ms
76,444 KB
testcase_14 AC 96 ms
76,860 KB
testcase_15 AC 76 ms
75,556 KB
testcase_16 AC 93 ms
76,568 KB
testcase_17 AC 99 ms
76,804 KB
testcase_18 AC 97 ms
76,744 KB
testcase_19 AC 132 ms
79,216 KB
testcase_20 AC 103 ms
77,580 KB
testcase_21 AC 100 ms
76,636 KB
testcase_22 AC 110 ms
78,084 KB
testcase_23 AC 71 ms
71,420 KB
testcase_24 AC 70 ms
71,148 KB
testcase_25 AC 70 ms
71,344 KB
testcase_26 AC 83 ms
76,220 KB
testcase_27 AC 98 ms
76,588 KB
testcase_28 AC 183 ms
85,412 KB
testcase_29 AC 168 ms
85,552 KB
testcase_30 AC 181 ms
85,432 KB
testcase_31 AC 72 ms
71,460 KB
testcase_32 AC 311 ms
85,860 KB
testcase_33 AC 274 ms
85,784 KB
testcase_34 AC 177 ms
85,724 KB
testcase_35 AC 235 ms
85,868 KB
testcase_36 AC 272 ms
85,404 KB
testcase_37 AC 209 ms
85,584 KB
testcase_38 AC 182 ms
85,592 KB
testcase_39 AC 194 ms
85,992 KB
testcase_40 AC 146 ms
85,332 KB
testcase_41 AC 260 ms
85,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# dp[S][T] = 今までに詰めたおもちゃの集合がS、おもちゃ箱の集合がTのとき、
# Tの最後の箱のスペースの大きさ

import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int,readline().split()))
M = int(readline())
B = list(map(int,readline().split()))

dp = [[-1] * (1 << M) for i in range(1 << N)]
dp[0][0] = 0
for S in range(1 << N):
  for T in range(1 << M):
    if dp[S][T] == -1:
      continue
    for toy in range(N): # 次に詰めるおもちゃ
      if (S >> toy) & 1:
        continue
      next_S = S | (1 << toy)
      if dp[S][T] >= A[toy]: # 今のスペースに詰められる
        if dp[next_S][T] < dp[S][T] - A[toy]:
          dp[next_S][T] = dp[S][T] - A[toy]
      else: # 詰められないので新しいおもちゃ箱を選ぶ
#        print(bin(S)[2:].zfill(N), bin(T)[2:].zfill(M), "新しいおもちゃ箱選ぶ")
        for box in range(M):
          if (T >> box) & 1:
#            print(box,"は使用済み")
            continue
          if B[box] < A[toy]: # 詰められない
#            print(B[box],"に",A[toy],"は詰められない")
            continue
          next_T = T | (1 << box)
          if dp[next_S][next_T] < B[box] - A[toy]:
            dp[next_S][next_T] = B[box] - A[toy]
#            print("next_S",bin(next_S)[2:].zfill(N),"next_T",bin(next_T)[2:].zfill(M),"空きは",B[box] - A[toy])

            
#for d in dp:
#  print(d)

def popcnt(x):
  res = 0
  while x:
    if x & 1:
      res += 1
    x >>= 1
  return res

ans = M + 1
# dp[-1][おもちゃ箱]の状態で、-1で無い物を探す。
# おもちゃ箱に立っているbitが最も少ないものが答え
for T in range(1 << M):
  if dp[-1][T] != -1:
#    print(bin(T)[2:].zfill(M),"のとき空きは",dp[-1][T])
    bits = popcnt(T)
    if ans > bits:
      ans = bits
    
if ans == M + 1:
  print(-1)
else:
  print(ans)
0