結果

問題 No.43 野球の試合
ユーザー tshigtshig
提出日時 2016-08-05 17:24:27
言語 Ruby
(3.3.0)
結果
AC  
実行時間 790 ms / 5,000 ms
コード長 909 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 11,516 KB
実行使用メモリ 27,572 KB
最終ジャッジ日時 2023-08-27 06:34:10
合計ジャッジ時間 2,426 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,264 KB
testcase_01 AC 83 ms
15,040 KB
testcase_02 AC 82 ms
15,300 KB
testcase_03 AC 81 ms
15,160 KB
testcase_04 AC 83 ms
15,204 KB
testcase_05 AC 83 ms
15,068 KB
testcase_06 AC 88 ms
15,212 KB
testcase_07 AC 790 ms
27,572 KB
testcase_08 AC 81 ms
15,252 KB
testcase_09 AC 83 ms
15,140 KB
testcase_10 AC 82 ms
15,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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
0