結果

問題 No.1950 片道きゃっちぼーる
ユーザー tomeruntomerun
提出日時 2022-05-20 22:24:04
言語 Crystal
(1.11.2)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 1,452 bytes
コンパイル時間 16,218 ms
コンパイル使用メモリ 290,376 KB
実行使用メモリ 57,144 KB
最終ジャッジ日時 2023-10-20 13:09:40
合計ジャッジ時間 21,833 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 215 ms
56,928 KB
testcase_04 AC 200 ms
44,448 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 181 ms
56,748 KB
testcase_07 AC 155 ms
43,472 KB
testcase_08 AC 160 ms
48,716 KB
testcase_09 AC 191 ms
55,240 KB
testcase_10 AC 175 ms
49,252 KB
testcase_11 AC 175 ms
49,268 KB
testcase_12 AC 184 ms
49,268 KB
testcase_13 AC 224 ms
47,728 KB
testcase_14 AC 200 ms
44,296 KB
testcase_15 AC 371 ms
57,144 KB
testcase_16 AC 187 ms
45,956 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 193 ms
51,976 KB
testcase_19 AC 362 ms
56,040 KB
testcase_20 AC 151 ms
48,716 KB
testcase_21 AC 188 ms
50,252 KB
testcase_22 AC 188 ms
43,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = read_line.to_i
x = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
g = Array.new(n) { [] of Int32 }
g_rev = Array.new(n) { [] of Int32 }
pos = Hash(Int32, Int32).new
n.times do |i|
  pos[x[i]] = i
end
n.times do |i|
  if pos.has_key?(x[i] - a[i])
    g_rev[pos[x[i] - a[i]]] << i
    g[i] << pos[x[i] - a[i]]
  end
  if pos.has_key?(x[i] + a[i])
    g_rev[pos[x[i] + a[i]]] << i
    g[i] << pos[x[i] + a[i]]
  end
end

stack = [] of Int32
visited = Array.new(n, false)
n.times do |i|
  scc_dfs(g, stack, visited, i)
end
visited.fill(false)
groups = [] of Array(Int32)
stack.reverse.each do |i|
  next if visited[i]
  group = [] of Int32
  scc_dfs_rev(g_rev, group, visited, i)
  groups << group
end

def scc_dfs(g, stack, visited, cur)
  return if visited[cur]
  visited[cur] = true
  g[cur].each do |c|
    scc_dfs(g, stack, visited, c)
  end
  stack << cur
end

def scc_dfs_rev(g_rev, list, visited, cur)
  return if visited[cur]
  visited[cur] = true
  g_rev[cur].each do |c|
    scc_dfs_rev(g_rev, list, visited, c)
  end
  list << cur
end

ans = Array.new(n, 0)
groups.reverse.each do |group|
  max = 0
  group.each do |i|
    max = {max, x[i] + a[i]}.max
    if pos.has_key?(x[i] + a[i])
      max = {max, ans[pos[x[i] + a[i]]]}.max
    end
    if pos.has_key?(x[i] - a[i])
      max = {max, ans[pos[x[i] - a[i]]]}.max
    end
  end
  group.each do |i|
    ans[i] = max
  end
end
puts n.times.map { |i| ans[i] - x[i] }.join("\n")
0