Created
October 16, 2018 05:02
-
-
Save whatalnk/ba2456141f15a40f941dc40a92c1e8aa to your computer and use it in GitHub Desktop.
AtCoder ABC #089 D. Practical Skill Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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