結果

問題 No.90 品物の並び替え
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2014-12-08 20:13:15
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,182 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 11,380 KB
実行使用メモリ 20,136 KB
最終ジャッジ日時 2023-09-02 11:45:42
合計ジャッジ時間 7,312 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
19,528 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class ListNumber
  attr_reader :list
  def initialize(n)
    @n = n
    @list = {}
    ((@n**(@n-2))..(@n**@n - 1)).each do |ls|
      flag = true
      @n.times do |i|
        unless num_index(ls, i)
          flag = false
          break
        end
      end
      if flag
        @list[ls] = 0
      end
    end
  end

  def num_index(ls, x)
    a = ls
    @n.times do |i|
      if x == a % @n
        return i
      else
        a = a / @n
      end
    end
    return nil
  end

  def match(ls, l, g)
    a = ls
    @n.times do |i|
      check = a % @n
      if check == l
        return true
      elsif check == g
        return false
      end
      a = a / @n
    end
    raise "ilegal #{ls.to_s(@n)}, #{l}, #{g}"
  end

  def add_score(l, g, s)
    @list.each do |ls, score|
      if match(ls, l, g)
        @list[ls] = score + s
      end
    end
  end

  def max_score
    @list.values.max
  end
end

n, m = gets.split.map(&:to_i)
list = []

m.times do
  list << gets.split.map(&:to_i)
end

lsnum = ListNumber.new(n)
list.each do |ss|
  lsnum.add_score(ss[0], ss[1], ss[2])
end
puts lsnum.max_score
0