結果

問題 No.15 カタログショッピング
ユーザー むらためむらため
提出日時 2019-01-31 20:44:33
言語 Nim
(2.0.2)
結果
AC  
実行時間 129 ms / 5,000 ms
コード長 1,190 bytes
コンパイル時間 3,587 ms
コンパイル使用メモリ 71,404 KB
実行使用メモリ 23,444 KB
最終ジャッジ日時 2023-09-14 03:20:09
合計ジャッジ時間 5,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,504 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 124 ms
23,444 KB
testcase_06 AC 126 ms
22,640 KB
testcase_07 AC 127 ms
23,264 KB
testcase_08 AC 125 ms
22,548 KB
testcase_09 AC 129 ms
23,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,algorithm,tables,strutils
template `^`(n:int) : int = (1 shl n)

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

proc build(P:seq[int],carry:int = 1) : Table[int,seq[seq[int]]] =
  result = initTable[int,seq[seq[int]]]()
  for x in 0 ..< ^P.len:
    var total = 0
    var pair = newSeq[int]()
    for j in 0..<P.len:
      if (^j and x) > 0 :
        total += P[j]
        pair &= j+carry
    if total notin result: result[total] = @[]
    result[total] &= pair

let n = scan()
let s = scan()
let P = newSeqWith(n,scan())
if n == 1: quit "1",0
let P1 = P[0..<n div 2]
let P2 = P[(n div 2) ..< n]
let I1 = P1.build(1)
let I2 = P2.build(1+(n div 2))
var answers = newSeq[seq[int]]()
for i2,strs in I2:
  if s - i2 notin I1 : continue
  for s1 in I1[s-i2]:
    for s2 in strs:
      answers &= s1 & s2
answers = answers.mapIt(it).sorted(proc(x,y:seq[int]):int =
  for i in 0..<min(x.len,y.len):
    if x[i] != y[i] : return x[i] - y[i]
  return x.len - y.len
)
for ans in answers:
  echo ans.mapIt($it).join(" ")
0