結果

問題 No.168 ものさし
ユーザー simansiman
提出日時 2017-09-17 22:12:57
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 2,922 bytes
コンパイル時間 39 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 61,568 KB
最終ジャッジ日時 2024-04-25 15:00:35
合計ジャッジ時間 9,941 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 307 ms
23,424 KB
testcase_01 AC 82 ms
12,288 KB
testcase_02 AC 85 ms
12,160 KB
testcase_03 AC 82 ms
12,288 KB
testcase_04 AC 78 ms
12,416 KB
testcase_05 WA -
testcase_06 AC 81 ms
12,416 KB
testcase_07 AC 79 ms
12,416 KB
testcase_08 WA -
testcase_09 AC 88 ms
12,672 KB
testcase_10 AC 105 ms
13,568 KB
testcase_11 AC 282 ms
23,040 KB
testcase_12 AC 769 ms
47,872 KB
testcase_13 AC 1,019 ms
61,312 KB
testcase_14 AC 1,009 ms
61,568 KB
testcase_15 AC 81 ms
12,288 KB
testcase_16 AC 87 ms
12,544 KB
testcase_17 AC 96 ms
12,928 KB
testcase_18 AC 127 ms
14,464 KB
testcase_19 AC 994 ms
59,776 KB
testcase_20 AC 1,025 ms
61,312 KB
testcase_21 AC 1,026 ms
61,440 KB
testcase_22 AC 1,036 ms
61,440 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:142: warning: assigned but unused variable - check_list
Syntax OK

ソースコード

diff #

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

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

        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
      elsif @nodes[left]
        if (@nodes[left].last <=> @nodes[index].last).public_send(@operator.last, 0)
          @nodes[index], @nodes[left] = @nodes[left], @nodes[index]
          index = left
        else
          break
        end
      elsif @nodes[right]
        if (@nodes[right].last <=> @nodes[index].last).public_send(@operator.last, 0)
          @nodes[index], @nodes[right] = @nodes[right], @nodes[index]
          index = right
        else
          break
        end
      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)

    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