結果

問題 No.4 おもりと天秤
ユーザー DialBird
提出日時 2019-07-09 13:32:38
言語 Ruby
(3.4.1)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 875 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-10-12 01:06:52
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def calc(idx, total)
end

def main
  # 2 <= n <= 100
  n = gets.to_i
  # 0..n-1
  ws = gets.split.map(&:to_i)
  total = ws.inject(:+)

  return 'impossible' if total.odd?

  half = total / 2
  dp = Array.new(n) { Array.new(half + 1, 0) }

  q = [{ idx: -1, total: 0 }]

  res = 'impossible'
  while !q.empty?
    cur = q.shift

    if cur[:total] == half
      res = 'possible'
      break
    end

    next if cur[:idx] == n - 1

    if cur[:total] + ws[cur[:idx] + 1] <= half
      if dp[cur[:idx] + 1][cur[:total] + ws[cur[:idx] + 1]].zero?
        q << { idx: cur[:idx] + 1, total: cur[:total] + ws[cur[:idx] + 1] }
        dp[cur[:idx] + 1][cur[:total] + ws[cur[:idx] + 1]] = 1
      end
    end
    if dp[cur[:idx] + 1][cur[:total]].zero?
      q << { idx: cur[:idx] + 1, total: cur[:total] }
      dp[cur[:idx] + 1][cur[:total]] = 1
    end
  end

  res
end

puts main
0