結果

問題 No.4 おもりと天秤
ユーザー tshigtshig
提出日時 2016-08-04 12:27:28
言語 Ruby
(3.2.2)
結果
AC  
実行時間 442 ms / 5,000 ms
コード長 679 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 11,304 KB
実行使用メモリ 23,348 KB
最終ジャッジ日時 2023-09-08 16:48:33
合計ジャッジ時間 5,138 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,296 KB
testcase_01 AC 80 ms
15,088 KB
testcase_02 AC 84 ms
15,248 KB
testcase_03 AC 80 ms
15,140 KB
testcase_04 AC 85 ms
15,080 KB
testcase_05 AC 317 ms
20,636 KB
testcase_06 AC 79 ms
15,296 KB
testcase_07 AC 345 ms
23,224 KB
testcase_08 AC 82 ms
15,348 KB
testcase_09 AC 85 ms
15,096 KB
testcase_10 AC 370 ms
23,076 KB
testcase_11 AC 80 ms
15,156 KB
testcase_12 AC 81 ms
15,104 KB
testcase_13 AC 78 ms
15,248 KB
testcase_14 AC 80 ms
15,160 KB
testcase_15 AC 79 ms
15,172 KB
testcase_16 AC 79 ms
15,092 KB
testcase_17 AC 78 ms
15,104 KB
testcase_18 AC 442 ms
23,348 KB
testcase_19 AC 304 ms
20,612 KB
testcase_20 AC 299 ms
20,672 KB
testcase_21 AC 287 ms
20,568 KB
testcase_22 AC 315 ms
20,788 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0004
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n = args.shift.first.to_i
    @ws = args.shift.map(&:to_i)
    @memo = Hash.new
  end

  def calc
    total = @ws.inject(:+)
    return false unless total.even?
    @half = total / 2
    dp(0, 0)
  end

  def dp(k, sum)
    if k >= @n || sum > @half
      res = false
    elsif sum == @half
      res = true
    elsif !@memo[[k, sum]].nil?
      res = @memo[[k, sum]]
    else
      res = dp(k + 1, sum) || dp(k + 1, sum + @ws[k])
    end
    @memo[[k, sum]] = res
  end

  def run
    calc ? 'possible' : 'impossible'
  end
end

puts Calc0004.new(STDIN.readlines).run if __FILE__ == $0
0