結果

問題 No.1170 Never Want to Walk
ユーザー Chihaya_chanChihaya_chan
提出日時 2020-09-05 00:13:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 626 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 113,368 KB
最終ジャッジ日時 2023-08-17 20:58:24
合計ジャッジ時間 13,054 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,276 KB
testcase_01 AC 76 ms
71,560 KB
testcase_02 AC 74 ms
71,460 KB
testcase_03 AC 76 ms
71,240 KB
testcase_04 AC 76 ms
71,404 KB
testcase_05 AC 77 ms
71,276 KB
testcase_06 AC 76 ms
71,388 KB
testcase_07 AC 76 ms
71,616 KB
testcase_08 AC 74 ms
71,280 KB
testcase_09 AC 74 ms
71,456 KB
testcase_10 AC 74 ms
71,428 KB
testcase_11 AC 76 ms
71,340 KB
testcase_12 AC 94 ms
76,008 KB
testcase_13 AC 98 ms
75,824 KB
testcase_14 AC 96 ms
76,312 KB
testcase_15 AC 92 ms
76,004 KB
testcase_16 AC 94 ms
76,180 KB
testcase_17 AC 97 ms
76,356 KB
testcase_18 AC 93 ms
76,208 KB
testcase_19 AC 93 ms
75,848 KB
testcase_20 AC 97 ms
76,324 KB
testcase_21 AC 94 ms
75,844 KB
testcase_22 AC 100 ms
75,832 KB
testcase_23 AC 96 ms
76,068 KB
testcase_24 AC 96 ms
75,960 KB
testcase_25 AC 95 ms
76,224 KB
testcase_26 AC 96 ms
75,816 KB
testcase_27 AC 529 ms
110,016 KB
testcase_28 AC 518 ms
110,664 KB
testcase_29 AC 556 ms
108,940 KB
testcase_30 AC 533 ms
111,236 KB
testcase_31 AC 533 ms
110,900 KB
testcase_32 AC 455 ms
109,484 KB
testcase_33 AC 626 ms
111,148 KB
testcase_34 AC 458 ms
111,380 KB
testcase_35 AC 458 ms
110,132 KB
testcase_36 AC 411 ms
108,384 KB
testcase_37 AC 476 ms
108,824 KB
testcase_38 AC 494 ms
113,368 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