結果

問題 No.90 品物の並び替え
ユーザー tshigitshigi
提出日時 2016-08-01 17:41:18
言語 Ruby
(3.2.2)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 728 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 11,380 KB
実行使用メモリ 15,692 KB
最終ジャッジ日時 2023-08-06 18:51:17
合計ジャッジ時間 2,319 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,264 KB
testcase_01 AC 88 ms
15,128 KB
testcase_02 AC 83 ms
15,328 KB
testcase_03 AC 86 ms
15,036 KB
testcase_04 AC 85 ms
15,116 KB
testcase_05 AC 91 ms
15,464 KB
testcase_06 AC 87 ms
15,432 KB
testcase_07 AC 87 ms
15,328 KB
testcase_08 AC 84 ms
15,316 KB
testcase_09 AC 94 ms
15,692 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

inputs = STDIN.readlines.map(&:chomp)
N, M = inputs[0].split(/\s+/).map(&:to_i)
SCORES = inputs[1..-1].map { |line| line.split(/\s+/).map(&:to_i) }

class Calc
  def initialize
    @memos = Hash.new
    @merchandises = (0...N).to_a
    @score_table = Hash.new { |h, k| h[k] = Hash.new }
    SCORES.each do |t|
      @score_table[t[0]][t[1]] = t[2]
    end
  end

  def run
    dp([])
  end

  def dp(used)
    if (@merchandises - used).empty?
      res = 0
    elsif @memos[used]
      res = @memos[used]
    else
      res = (@merchandises - used).map { |m|
        used.map { |u| @score_table[u][m].to_i }.inject(:+).to_i + dp((used + [m]).sort)
      }.max
    end
    @memos[used] = res
    res
  end
end

puts Calc.new.run
0