結果

問題 No.168 ものさし
ユーザー snipsnipsnipsnipsnipsnip
提出日時 2015-03-24 19:44:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,384 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 11,220 KB
実行使用メモリ 24,000 KB
最終ジャッジ日時 2023-08-25 20:54:01
合計ジャッジ時間 8,056 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 370 ms
17,564 KB
testcase_01 AC 82 ms
15,148 KB
testcase_02 AC 80 ms
15,284 KB
testcase_03 AC 81 ms
15,272 KB
testcase_04 AC 80 ms
15,316 KB
testcase_05 AC 81 ms
15,352 KB
testcase_06 AC 81 ms
15,184 KB
testcase_07 AC 81 ms
15,328 KB
testcase_08 AC 82 ms
15,332 KB
testcase_09 AC 84 ms
15,300 KB
testcase_10 AC 100 ms
15,348 KB
testcase_11 AC 247 ms
17,100 KB
testcase_12 AC 460 ms
21,228 KB
testcase_13 AC 507 ms
23,232 KB
testcase_14 AC 102 ms
23,116 KB
testcase_15 AC 82 ms
15,212 KB
testcase_16 AC 86 ms
15,440 KB
testcase_17 AC 100 ms
15,552 KB
testcase_18 AC 96 ms
15,680 KB
testcase_19 AC 731 ms
23,504 KB
testcase_20 AC 528 ms
23,664 KB
testcase_21 AC 1,384 ms
23,800 KB
testcase_22 AC 697 ms
24,000 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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