結果

問題 No.4 おもりと天秤
ユーザー DialBirdDialBird
提出日時 2019-07-09 13:32:38
言語 Ruby
(3.3.0)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 875 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-04-20 06:01:52
合計ジャッジ時間 2,937 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
12,288 KB
testcase_01 AC 78 ms
12,160 KB
testcase_02 AC 76 ms
12,160 KB
testcase_03 AC 72 ms
12,160 KB
testcase_04 AC 73 ms
12,288 KB
testcase_05 AC 125 ms
15,232 KB
testcase_06 AC 76 ms
12,160 KB
testcase_07 AC 128 ms
15,104 KB
testcase_08 AC 71 ms
12,288 KB
testcase_09 AC 75 ms
15,872 KB
testcase_10 AC 124 ms
15,360 KB
testcase_11 AC 72 ms
12,288 KB
testcase_12 AC 73 ms
12,160 KB
testcase_13 AC 72 ms
12,288 KB
testcase_14 AC 73 ms
12,288 KB
testcase_15 AC 73 ms
12,160 KB
testcase_16 AC 77 ms
12,160 KB
testcase_17 AC 72 ms
12,160 KB
testcase_18 AC 148 ms
15,232 KB
testcase_19 AC 116 ms
14,848 KB
testcase_20 AC 121 ms
15,104 KB
testcase_21 AC 118 ms
14,848 KB
testcase_22 AC 115 ms
14,848 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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