結果

問題 No.168 ものさし
ユーザー simansiman
提出日時 2017-09-18 07:20:23
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 2,684 bytes
コンパイル時間 42 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 65,920 KB
最終ジャッジ日時 2024-04-25 15:18:03
合計ジャッジ時間 11,403 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 360 ms
23,680 KB
testcase_01 AC 93 ms
12,288 KB
testcase_02 AC 92 ms
12,544 KB
testcase_03 AC 93 ms
12,544 KB
testcase_04 AC 99 ms
12,416 KB
testcase_05 WA -
testcase_06 AC 94 ms
12,288 KB
testcase_07 AC 92 ms
12,544 KB
testcase_08 WA -
testcase_09 AC 102 ms
12,800 KB
testcase_10 AC 122 ms
13,568 KB
testcase_11 AC 321 ms
23,168 KB
testcase_12 AC 872 ms
47,872 KB
testcase_13 AC 1,103 ms
65,920 KB
testcase_14 AC 1,084 ms
65,920 KB
testcase_15 AC 96 ms
12,416 KB
testcase_16 AC 101 ms
12,544 KB
testcase_17 AC 113 ms
13,184 KB
testcase_18 AC 149 ms
14,592 KB
testcase_19 AC 1,144 ms
59,904 KB
testcase_20 AC 1,172 ms
61,568 KB
testcase_21 AC 1,187 ms
61,568 KB
testcase_22 AC 1,181 ms
61,312 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:138: warning: assigned but unused variable - check_list
Syntax OK

ソースコード

diff #

require 'bigdecimal'

class UnionFind
  def initialize(n)
    @size = Array.new(n, 1)
    @parent = []

    n.times do |i|
      @parent[i] = i
    end
  end

  def find(x)
    if @parent[x] == x
      x
    else
      @parent[x] = find(@parent[x])
    end
  end

  def unite(x, y)
    x = find(x)
    y = find(y)
    return if x == y

    @parent[y] = x
    @size[x] += @size[y]
  end

  def same(x, y)
    find(x) == find(y)
  end

  def size(x)
    @size[find(x)]
  end
end

class PriorityQueue
  attr_reader :size

  def initialize(order: :asc)
    @size = 0
    @nodes = []
    @operator = (order == :asc) ? [:>, :<] : [:<, :>]
  end

  def empty?
    @size.zero?
  end

  def push(node)
    if @nodes[0].nil?
      @nodes[0] = node
    else
      index = size
      @nodes[index] = node

      loop {
        parent = (index-1)/2

        if (@nodes[parent].last <=> node.last).public_send(@operator.first, 0)
          @nodes[index], @nodes[parent] = @nodes[parent], @nodes[index]
          index = parent

          break if index.zero?
        else
          break
        end
      }
    end

    @size += 1
  end

  alias_method :<<, :push

  def pop
    node = @nodes.first
    @nodes[0], @nodes[size - 1] = @nodes[size - 1], nil
    index = 0

    loop {
      left = 2*index+1
      right = 2*index+2

      target = if @nodes[left] && @nodes[right]
                 target = (@nodes[left].last <=> @nodes[right].last).public_send(@operator.last, 0) ? left : right
               elsif @nodes[left]
                 target = left
               elsif @nodes[right]
                 target = right
               else
                 nil
               end

      break if target.nil?

      if (@nodes[target].last <=> @nodes[index].last).public_send(@operator.last, 0)
        @nodes[index], @nodes[target] = @nodes[target], @nodes[index]
        index = target
      else
        break
      end
    }

    @size -= 1
    node.first
  end

  def top
    @nodes.first
  end
end

N = gets.to_i

points = []

N.times do
  points << gets.chomp.split.map(&:to_i)
end

pque = PriorityQueue.new

0.upto(N-2) do |i|
  (i+1).upto(N-1) do |j|
    p1 = points[i]
    p2 = points[j]

    dist = Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
    # dist = BigDecimal((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2).sqrt(10)

    pque.push([[i, j, dist], dist])
  end
end

check_list = {}
max_d = 0
uf = UnionFind.new(N)

until pque.empty?
  i, j, dist = pque.pop

  if uf.find(i) != uf.find(j)
    uf.unite(i, j)
    max_d = [max_d, dist].max

    break if uf.find(0) == uf.find(N-1)
  end
end

if max_d % 10 == 0
  puts max_d.to_i
else
  puts 10 * (max_d.to_i / 10 + 1)
end
0