結果

問題 No.1170 Never Want to Walk
ユーザー simansiman
提出日時 2020-10-27 16:00:18
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,097 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 11,300 KB
実行使用メモリ 36,276 KB
最終ジャッジ日時 2023-09-29 03:18:01
合計ジャッジ時間 11,907 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,268 KB
testcase_01 AC 82 ms
15,268 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 85 ms
15,336 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 547 ms
35,976 KB
testcase_34 WA -
testcase_35 AC 544 ms
36,144 KB
testcase_36 AC 534 ms
35,988 KB
testcase_37 AC 526 ms
35,904 KB
testcase_38 AC 545 ms
36,092 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

    (0..n).each 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

    if @rank[x] < @rank[y]
      @parent[x] = y
      @size[y] += @size[x]
    else
      @parent[y] = x
      @size[x] += @size[y]

      @rank[x] += 1 if @rank[x] == @rank[y]
    end
  end

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

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

N, A, B = gets.split.map(&:to_i)
X = gets.split.map(&:to_i)
uf = UnionFind.new(N)

l = 0
r = 1

while l < r
  a = X[l]
  b = X[r]
  d = b - a

  if A <= d && d <= B
    uf.unite(l, r)
  end

  if d < A
    if r + 1 < N
      r += 1
    else
      l += 1
    end
  elsif B < d
    l += 1
  else
    if r + 1 < N
      if X[r + 1] - a <= B
        r += 1
      else
        l += 1
      end
    else
      l += 1
    end
  end
end

N.times do |i|
  puts uf.size(i)
end
0