結果

問題 No.1170 Never Want to Walk
ユーザー Chihaya_chanChihaya_chan
提出日時 2020-09-05 00:13:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 527 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 109,584 KB
最終ジャッジ日時 2024-05-05 03:49:50
合計ジャッジ時間 9,707 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 40 ms
52,864 KB
testcase_02 AC 40 ms
52,608 KB
testcase_03 AC 38 ms
52,608 KB
testcase_04 AC 38 ms
52,736 KB
testcase_05 AC 38 ms
52,736 KB
testcase_06 AC 36 ms
52,352 KB
testcase_07 AC 36 ms
52,864 KB
testcase_08 AC 37 ms
52,864 KB
testcase_09 AC 36 ms
52,608 KB
testcase_10 AC 36 ms
52,736 KB
testcase_11 AC 37 ms
52,480 KB
testcase_12 AC 66 ms
66,816 KB
testcase_13 AC 65 ms
68,992 KB
testcase_14 AC 63 ms
68,992 KB
testcase_15 AC 59 ms
66,560 KB
testcase_16 AC 61 ms
68,096 KB
testcase_17 AC 65 ms
68,992 KB
testcase_18 AC 62 ms
66,432 KB
testcase_19 AC 62 ms
67,456 KB
testcase_20 AC 75 ms
69,888 KB
testcase_21 AC 69 ms
66,816 KB
testcase_22 AC 69 ms
70,400 KB
testcase_23 AC 69 ms
68,992 KB
testcase_24 AC 67 ms
69,632 KB
testcase_25 AC 64 ms
69,248 KB
testcase_26 AC 63 ms
69,248 KB
testcase_27 AC 440 ms
108,148 KB
testcase_28 AC 445 ms
106,760 KB
testcase_29 AC 478 ms
107,528 KB
testcase_30 AC 456 ms
108,364 KB
testcase_31 AC 483 ms
108,560 KB
testcase_32 AC 379 ms
108,368 KB
testcase_33 AC 527 ms
109,584 KB
testcase_34 AC 397 ms
108,052 KB
testcase_35 AC 389 ms
109,032 KB
testcase_36 AC 354 ms
106,160 KB
testcase_37 AC 404 ms
107,328 KB
testcase_38 AC 426 ms
108,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * (n+1)

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n + 1) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    import bisect
    N, A, B = map(int, input().split())
    D = list(map(int, input().split()))
    Uni = UnionFind(N+10)
    que = []
    for i, v in enumerate(D):
        l = bisect.bisect_right(D, v+A-1) + 1
        r = bisect.bisect_right(D, v+B)
        if l<=r:
            que.append((l, r))
            Uni.union(i+1, l)
            Uni.union(i+1, r)

    Left, Right = que[0]
    for i in range(1, len(que)):
        a, b = que[i]
        if Left <= a <= Right:
            Right = b
        elif a > Right:
            for j in range(Left, Right):
                Uni.union(j, j+1)
            Left = a
            Right = b
    for j in range(Left, Right):
        Uni.union(j, j+1)
    for i in range(1, N+1):
        print(Uni.size(i))
    return


if __name__ == "__main__":
    main()
0