結果
| 問題 | No.43 野球の試合 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-08-05 17:24:27 | 
| 言語 | Ruby (3.4.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 868 ms / 5,000 ms | 
| コード長 | 909 bytes | 
| コンパイル時間 | 465 ms | 
| コンパイル使用メモリ | 7,424 KB | 
| 実行使用メモリ | 24,704 KB | 
| 最終ジャッジ日時 | 2024-12-26 17:34:44 | 
| 合計ジャッジ時間 | 3,397 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 7 | 
コンパイルメッセージ
Syntax OK
ソースコード
class Calc0043
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n = args.shift.first.to_i
    @wl = args.map(&:first).map { |l| l.split(//) }
  end
  def run
    no_games = (0...@n).flat_map { |i|
      ((i + 1)...@n).map { |j|
        @wl[i][j] == '-' ? [i, j] : nil
      }
    }.compact
    if no_games.empty?
      calc_rank(@wl)
    else
      cands = no_games.map { |n| ['x', 'o'] }
      cands.first.product(*cands.drop(1)).map { |cand|
        calc(no_games, cand)
      }.min
    end
  end
  def calc(no_games, cand)
    wl = @wl.map { |r| r.clone }
    no_games.zip(cand) { |(i, j), c|
      wl[i][j] = c
      wl[j][i] = c == 'o' ? 'x' : 'o'
    }
    calc_rank(wl)
  end
  def calc_rank(wl)
    ws = wl.map { |r| r.count { |c| c == 'o' } }
    w0 = ws[0]
    ws.uniq.sort.reverse.index(w0) + 1
  end
end
puts Calc0043.new(STDIN.readlines).run if __FILE__ == $0
            
            
            
        