結果

問題 No.4 おもりと天秤
ユーザー DialBirdDialBird
提出日時 2017-02-11 12:08:46
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 590 bytes
コンパイル時間 64 ms
コンパイル使用メモリ 11,568 KB
実行使用メモリ 15,352 KB
最終ジャッジ日時 2023-08-28 12:49:53
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,140 KB
testcase_01 AC 83 ms
15,304 KB
testcase_02 AC 83 ms
15,244 KB
testcase_03 AC 82 ms
15,248 KB
testcase_04 AC 82 ms
15,160 KB
testcase_05 WA -
testcase_06 AC 82 ms
15,112 KB
testcase_07 WA -
testcase_08 AC 84 ms
15,160 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 82 ms
15,344 KB
testcase_12 AC 83 ms
15,136 KB
testcase_13 AC 83 ms
15,164 KB
testcase_14 AC 83 ms
15,252 KB
testcase_15 AC 82 ms
15,168 KB
testcase_16 AC 82 ms
15,096 KB
testcase_17 AC 83 ms
15,228 KB
testcase_18 AC 85 ms
15,316 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
@w_list = gets.chomp.split.map(&:to_i)
@half = 0
@dp = Array.new(100, Array.new(10001, true))

def solve(idx, total)
  return false if idx >= N || total > @half
  return @dp[idx][total] if @dp[idx][total] == false

  new_total = total + @w_list[idx]
  return true if new_total == @half

  return true if solve(idx + 1, total) || solve(idx + 1, new_total)
  return @dp[idx][total] = false
end

def main
  total = @w_list.inject(:+)

  return "impossible" if total & 1 == 1

  @half = total >> 1
  @w_list.sort!

  return solve(0, 0) ? "possible" : "impossible"
end

puts main()
0