結果
| 問題 |
No.90 品物の並び替え
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-01 17:25:09 |
| 言語 | Ruby (3.4.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 831 bytes |
| コンパイル時間 | 137 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 12,800 KB |
| 最終ジャッジ日時 | 2024-11-06 21:40:36 |
| 合計ジャッジ時間 | 1,786 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 9 |
コンパイルメッセージ
Syntax OK
ソースコード
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 = Array.new(N) { 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(0, nil, @merchandises)
end
def dp(score, last, rest)
if rest.empty?
res = 0
elsif last && @memos[last][rest]
res = @memos[last][rest]
elsif last
res = rest.map { |m|
dp(@score_table[last][m].to_i, m, rest - [m])
}.max
else
res = rest.map { |m|
dp(0, m, rest - [m])
}.max
end
@memos[last][rest] = res if last
score + res
end
end
puts Calc.new.run