結果

問題 No.168 ものさし
ユーザー snipsnipsnip
提出日時 2015-03-24 19:34:42
言語 Ruby
(3.4.1)
結果
TLE  
実行時間 -
コード長 970 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 29,440 KB
最終ジャッジ日時 2024-06-29 00:33:33
合計ジャッジ時間 6,391 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
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]
    done = Set.new
    while i = stack.pop
      done << i
      points.each_index do |j|
        if !done.include?(j) && squared_distance_by_index(i, j) <= sqlen
          return true if j.equal?(goal)
          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