結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,156 KB
testcase_01 AC 82 ms
15,172 KB
testcase_02 AC 83 ms
15,340 KB
testcase_03 AC 84 ms
15,220 KB
testcase_04 AC 81 ms
15,308 KB
testcase_05 AC 84 ms
15,152 KB
testcase_06 AC 82 ms
15,124 KB
testcase_07 AC 82 ms
15,288 KB
testcase_08 AC 82 ms
15,156 KB
testcase_09 AC 84 ms
15,092 KB
testcase_10 AC 85 ms
15,040 KB
testcase_11 AC 83 ms
15,272 KB
testcase_12 AC 88 ms
15,152 KB
testcase_13 AC 86 ms
15,112 KB
testcase_14 AC 85 ms
15,112 KB
testcase_15 AC 84 ms
15,040 KB
testcase_16 AC 85 ms
15,356 KB
testcase_17 AC 85 ms
15,160 KB
testcase_18 AC 85 ms
15,120 KB
testcase_19 AC 85 ms
15,120 KB
testcase_20 AC 87 ms
15,120 KB
testcase_21 AC 86 ms
15,228 KB
testcase_22 AC 87 ms
15,116 KB
testcase_23 AC 86 ms
15,288 KB
testcase_24 AC 85 ms
15,168 KB
testcase_25 AC 86 ms
15,108 KB
testcase_26 AC 86 ms
15,348 KB
testcase_27 AC 541 ms
35,872 KB
testcase_28 AC 536 ms
35,796 KB
testcase_29 AC 540 ms
36,308 KB
testcase_30 AC 553 ms
36,108 KB
testcase_31 AC 535 ms
35,760 KB
testcase_32 AC 533 ms
35,944 KB
testcase_33 AC 541 ms
35,752 KB
testcase_34 AC 539 ms
36,088 KB
testcase_35 AC 547 ms
36,020 KB
testcase_36 AC 537 ms
35,924 KB
testcase_37 AC 530 ms
35,840 KB
testcase_38 AC 546 ms
36,200 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 l != r && 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