結果

問題 No.168 ものさし
ユーザー snipsnipsnip
提出日時 2015-03-24 19:44:36
言語 Ruby
(3.4.1)
結果
AC  
実行時間 1,595 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 23,808 KB
最終ジャッジ日時 2024-12-24 07:06:17
合計ジャッジ時間 8,798 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'set'

Problem168 = Struct.new(:points) do
  def solve
    @distance_cache = []
    w = isqrt(squared_distance_by_index(0, points.size - 1)) / 10
    p 10 * (1..w + 1).bsearch {|d| reach? d }
  end

  private

  def isqrt(x)
    (1..x).bsearch {|r| x <= r ** 2 }
  end

  def reach?(d)
    sqlen = (d * 10) ** 2
    goal = points.size - 1
    stack = [0]
    todo = Set.new(1..goal)
    while i = stack.pop
      todo.each do |j|
        if squared_distance_by_index(i, j) <= sqlen
          return true if j.equal?(goal)
          todo.delete j
          stack << j
        end
      end
    end
    false
  end

  def squared_distance_by_index(ia, ib)
    ia, ib = ib, ia if ib < ia
    @distance_cache[ia * points.size + ib] ||= squared_distance(points[ia], points[ib])
  end

  def squared_distance(a, b)
    ax, ay = a
    bx, by = b
    (ax - bx) ** 2 + (ay - by) ** 2
  end
end

Problem168.new((1..gets.to_i).map{gets.split.map(&:to_i)}).solve
0