結果

問題 No.168 ものさし
ユーザー snipsnipsnipsnipsnipsnip
提出日時 2015-03-24 19:34:42
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 970 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 11,416 KB
実行使用メモリ 28,804 KB
最終ジャッジ日時 2023-09-11 09:57:28
合計ジャッジ時間 6,973 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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