結果

問題 No.15 カタログショッピング
ユーザー vjudge1
提出日時 2025-09-08 10:25:24
言語 Crystal
(1.14.0)
結果
AC  
実行時間 1,906 ms / 5,000 ms
コード長 723 bytes
コンパイル時間 16,146 ms
コンパイル使用メモリ 317,212 KB
実行使用メモリ 17,424 KB
最終ジャッジ日時 2025-09-08 10:25:51
合計ジャッジ時間 25,753 ms
ジャッジサーバーID
(参考情報)
judge / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(m)
  p = Array.new(m) { gets.not_nil!.to_i }

  res = [] of Tuple(Int32, Array(Int32))
  (0...(1 << m)).each do |bit|
    cnt = 0
    v = [] of Int32
    m.times do |i|
      if (bit >> i) & 1 == 1
        cnt += p[i]
        v << i
      end
    end
    res << {cnt, v}
  end

  res
end

n, s = gets.not_nil!.split.map(&.to_i)
p1 = solve(n // 2)
p2 = solve(n - n // 2)

p2.sort_by! { |(sum, _)| sum }

ans = [] of Array(Int32)
p1.each do |s1, v1|
  target = s - s1
  # Find all elements in p2 with the target sum
  p2.each do |s2, v2|
    if s2 == target
      res = v1.dup
      v2.each { |x| res << x + n // 2 }
      ans << res
    end
  end
end

ans.sort.each do |v|
  puts v.map { |x| x + 1 }.join(" ")
end
0