結果

問題 No.572 妖精の演奏
ユーザー simansiman
提出日時 2022-11-24 18:49:37
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,862 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-10-01 08:21:17
合計ジャッジ時間 5,061 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
12,288 KB
testcase_01 AC 87 ms
12,416 KB
testcase_02 AC 78 ms
12,288 KB
testcase_03 AC 78 ms
12,160 KB
testcase_04 AC 76 ms
12,160 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 103 ms
12,160 KB
testcase_08 AC 126 ms
12,032 KB
testcase_09 AC 133 ms
12,416 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 349 ms
12,928 KB
testcase_15 AC 460 ms
13,440 KB
testcase_16 AC 361 ms
13,056 KB
testcase_17 AC 562 ms
13,568 KB
testcase_18 AC 102 ms
12,288 KB
testcase_19 AC 75 ms
12,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:91: warning: assigned but unused variable - np
Main.rb:56: warning: assigned but unused variable - start_pos
Syntax OK

ソースコード

diff #

N = gets.to_i
M = gets.to_i
A = M.times.map { gets.split.map(&:to_i) }

def f(start_pos, len = M)
  dp = Array.new(len + 1) { Array.new(M, 0) }

  len.times do |i|
    M.times do |from|
      next if i == 0 && start_pos != from

      a = dp[i][from]

      M.times do |to|
        na = a + A[from][to]

        if dp[i + 1][to] < na
          dp[i + 1][to] = na
        end
      end
    end
  end

  max_a = -1
  best_i = -1

  dp[len].each_with_index do |a, i|
    if max_a < a
      max_a = a
      best_i = i
    end
  end

  [best_i, max_a]
end

def find_first_step
  max_a = -1
  first_step = -1

  M.times do |i|
    _, a = f(i, M - 1)

    if max_a < a
      max_a = a
      first_step = i
    end
  end

  first_step
end

remain = N - 1

cur_pos = find_first_step
start_pos = cur_pos
pos = cur_pos
graph = Hash.new

M.times do
  next_pos, cost = f(pos, M)

  graph[pos] = [next_pos, cost]
  pos = next_pos
end

if remain > M * M
  visited = Hash.new(false)
  positions = []
  loop_cost = 0

  while !visited[cur_pos]
    positions << cur_pos
    visited[cur_pos] = true
    cur_pos, _ = graph[cur_pos]
  end

  ans = 0
  rindex = positions.rindex(cur_pos)
  head, main = positions[0...rindex], positions[rindex..]

  head.each_cons(2) do |from, to|
    _, cost = graph[from]
    ans += cost
    remain -= M
  end

  # pp [:remain, remain]
  loop_cost = 0
  main.each do |from|
    np, cost = graph[from]
    loop_cost += cost
  end

  loop_size = main.size
  loop_cnt = remain / (M * loop_size)
  remain -= M * loop_cnt * loop_size
  # pp [:remain, remain, :loop_size, loop_size, :loop_cnt, loop_cnt, :loop_cost, loop_cost]
  ans += loop_cnt * loop_cost
  cur_pos = main[0]

  _, last_cost = f(cur_pos, remain)
  ans += last_cost

  puts ans
else
  max_a = -1

  M.times do |i|
    _, a = f(i, N - 1)
    max_a = a if max_a < a
  end

  puts max_a
end
0