結果

問題 No.50 おもちゃ箱
ユーザー むらため
提出日時 2017-08-15 15:21:14
言語 Nim
(2.2.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,093 bytes
コンパイル時間 3,731 ms
コンパイル使用メモリ 75,136 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-30 02:37:30
合計ジャッジ時間 4,741 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 50) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(23, 57) Warning: Deprecated since v0.20, use 'toHashSet'; toSet is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 50) Warning: imported and not used: 'future' [UnusedImport]
/home/judge/data/code/Main.nim(2, 13) Warning: imported and not used: 'tables' [UnusedImport]
/home/judge/data/code/Main.nim(1, 26) Warning: imported and not used: 'strscans' [UnusedImport]
/home/judge/data/code/Main.nim(1, 57) Warning: imported and not used: 'macros' [UnusedImport]

ソースコード

diff #

import sequtils,strutils,strscans,algorithm,math,future,macros
import sets,tables
template get*():string = stdin.readLine() #.strip()
template `max=`*(x,y:typed):void = x = max(x,y)
template optPow{`^`(2,n)}(n:int) : int = 1 shl n
template If*(ex:untyped):untyped = (if not(ex) : continue)
proc enumerate[T](arr:seq[T]): seq[tuple[i:int,val:T]] =
  result = @[]; for i,a in arr: result &= (i,a)

let
  N = get().parseInt()
  A = get().split().map(parseInt).sorted(cmp,Descending)
  M = get().parseInt()
  B = get().split().map(parseInt).sorted(cmp,Descending)

var state = newSeqWith(2^N,0)
for n in 0 ..< 2^N:
  for ia in 0 ..< N:
    If: (n and 2^ia) == 2^ia
    state[n] += A[ia] # 各状態の体積の和

proc getOKState(b:int): HashSet[int] =
  state.enumerate().filterIt(b-it.val >= 0).mapIt(it.i).toSet()

var ans = B[0].getOKState()
if 2^N-1 in ans:
  echo 1
  quit()

for i,b in B[1..^1]:
  var n_ans = ans
  for x in b.getOKState().items:
    for y in ans.items:
      If: (x and y) == 0
      n_ans.incl(x xor y)
  ans = n_ans
  if 2^N-1 in ans:
    echo i + 2
    quit()
echo -1
0