結果

問題 No.4 おもりと天秤
ユーザー Naoki_M_Naoki_M_
提出日時 2016-09-07 09:39:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 176 ms / 5,000 ms
コード長 440 bytes
コンパイル時間 461 ms
コンパイル使用メモリ 11,176 KB
実行使用メモリ 15,620 KB
最終ジャッジ日時 2023-09-08 17:09:38
合計ジャッジ時間 3,815 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,316 KB
testcase_01 AC 80 ms
15,396 KB
testcase_02 AC 83 ms
15,536 KB
testcase_03 AC 81 ms
15,568 KB
testcase_04 AC 82 ms
15,288 KB
testcase_05 AC 149 ms
15,356 KB
testcase_06 AC 80 ms
15,604 KB
testcase_07 AC 151 ms
15,516 KB
testcase_08 AC 81 ms
15,564 KB
testcase_09 AC 176 ms
15,620 KB
testcase_10 AC 158 ms
15,408 KB
testcase_11 AC 81 ms
15,572 KB
testcase_12 AC 82 ms
15,544 KB
testcase_13 AC 79 ms
15,508 KB
testcase_14 AC 80 ms
15,496 KB
testcase_15 AC 80 ms
15,400 KB
testcase_16 AC 78 ms
15,412 KB
testcase_17 AC 80 ms
15,428 KB
testcase_18 AC 140 ms
15,408 KB
testcase_19 AC 144 ms
15,436 KB
testcase_20 AC 144 ms
15,432 KB
testcase_21 AC 140 ms
15,400 KB
testcase_22 AC 148 ms
15,424 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.chomp.to_i
ws = gets.chomp.split.map(&:to_i)

possible = Array.new(2){Array.new(10001, false)}
possible[0][0] = true

s = 0
(0...n).to_a.each do |i|
  w = ws[i]
  for j in 0..s
    possible[(i + 1) % 2][j] = false
  end
  for j in 0..s
    if possible[i % 2][j]
      possible[(i + 1) % 2][j + w] = true
      possible[(i + 1) % 2][(j - w).abs] = true
    end
  end
  s += w
end

puts possible[n % 2][0] ? 'possible' : 'impossible'
0