結果

問題 No.1170 Never Want to Walk
ユーザー tktk_snsntktk_snsn
提出日時 2020-08-14 22:29:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 876 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,260 KB
実行使用メモリ 132,252 KB
最終ジャッジ日時 2024-04-18 22:19:59
合計ジャッジ時間 13,281 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,504 KB
testcase_01 AC 48 ms
54,400 KB
testcase_02 AC 49 ms
54,272 KB
testcase_03 AC 48 ms
54,144 KB
testcase_04 AC 49 ms
54,016 KB
testcase_05 AC 49 ms
53,632 KB
testcase_06 AC 49 ms
53,888 KB
testcase_07 AC 50 ms
54,016 KB
testcase_08 AC 49 ms
53,888 KB
testcase_09 AC 49 ms
53,888 KB
testcase_10 AC 49 ms
54,272 KB
testcase_11 AC 49 ms
53,760 KB
testcase_12 AC 88 ms
72,832 KB
testcase_13 AC 94 ms
75,776 KB
testcase_14 AC 96 ms
75,520 KB
testcase_15 AC 87 ms
72,832 KB
testcase_16 AC 89 ms
73,984 KB
testcase_17 AC 95 ms
75,904 KB
testcase_18 AC 87 ms
72,576 KB
testcase_19 AC 96 ms
75,520 KB
testcase_20 AC 97 ms
75,520 KB
testcase_21 AC 89 ms
73,344 KB
testcase_22 AC 90 ms
75,776 KB
testcase_23 AC 97 ms
75,776 KB
testcase_24 AC 98 ms
75,648 KB
testcase_25 AC 98 ms
75,776 KB
testcase_26 AC 96 ms
76,160 KB
testcase_27 AC 763 ms
130,420 KB
testcase_28 AC 696 ms
130,024 KB
testcase_29 AC 876 ms
132,252 KB
testcase_30 AC 780 ms
131,148 KB
testcase_31 AC 778 ms
129,492 KB
testcase_32 AC 607 ms
129,372 KB
testcase_33 AC 742 ms
130,512 KB
testcase_34 AC 693 ms
130,120 KB
testcase_35 AC 658 ms
130,140 KB
testcase_36 AC 635 ms
130,212 KB
testcase_37 AC 680 ms
129,496 KB
testcase_38 AC 659 ms
130,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import deque


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数
        self.rank = [0] * (n + 1)

    def find(self, x):  # xの根となる要素番号を返す
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        return -self.root[self.find(x)]


N, A, B = map(int, input().split())
X = list(map(int, input().split()))
uf = UF_tree(N)

upper = []
lower = []
for i, xi in enumerate(X):
    l = bisect.bisect_left(X, xi + A)
    r = bisect.bisect_right(X, xi + B)
    if l < r:
        uf.unite(i, l)
    upper.append((l, r))

    l = bisect.bisect_left(X, xi - B)
    r = bisect.bisect_right(X, xi - A)
    if l < r:
        uf.unite(i, l)
    lower.append((l, r))


for p, q in zip(range(N - 1), range(1, N)):
    _, pr = upper[p]
    ql, _ = upper[q]
    if pr > ql:
        uf.unite(p, q)
    _, pr = lower[p]
    ql, _ = lower[q]
    if pr > ql:
        uf.unite(p, q)

for i in range(N):
    print(uf.size(i))
0