Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created October 16, 2018 05:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save whatalnk/ba2456141f15a40f941dc40a92c1e8aa to your computer and use it in GitHub Desktop.
AtCoder ABC #089 D. Practical Skill Test
h, w, d = gets.chomp.split(" ").map(&:to_i)
g = Array.new(h*w+1)
h.times do |i|
row = gets.chomp.split(" ").map(&:to_i)
w.times do |j|
a = row[j]
g[a] = [i, j]
end
end
a = Array.new(h*w+1, 0)
1.upto(h*w-d) do |i|
x0, y0 = g[i]
x1, y1 = g[i+d]
cost = (x1 - x0).abs + (y1 - y0).abs
a[i+d] = a[i] + cost
end
q = gets.chomp.to_i
q.times do
l, r = gets.chomp.split(" ").map(&:to_i)
if l == r
puts 0
else
puts a[r] - a[l]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment