結果

問題 No.1170 Never Want to Walk
ユーザー 👑 H20H20
提出日時 2021-11-23 23:19:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 112,036 KB
最終ジャッジ日時 2023-09-08 03:52:35
合計ジャッジ時間 11,898 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,444 KB
testcase_01 AC 90 ms
71,300 KB
testcase_02 AC 90 ms
71,652 KB
testcase_03 AC 90 ms
71,476 KB
testcase_04 AC 90 ms
71,744 KB
testcase_05 AC 91 ms
71,744 KB
testcase_06 AC 90 ms
71,616 KB
testcase_07 AC 91 ms
71,464 KB
testcase_08 AC 90 ms
71,840 KB
testcase_09 AC 90 ms
71,744 KB
testcase_10 AC 89 ms
71,756 KB
testcase_11 AC 90 ms
71,424 KB
testcase_12 AC 110 ms
76,848 KB
testcase_13 AC 113 ms
76,988 KB
testcase_14 AC 115 ms
76,840 KB
testcase_15 AC 113 ms
76,632 KB
testcase_16 AC 111 ms
76,836 KB
testcase_17 AC 113 ms
76,968 KB
testcase_18 AC 112 ms
76,632 KB
testcase_19 AC 112 ms
76,628 KB
testcase_20 AC 112 ms
76,500 KB
testcase_21 AC 112 ms
76,748 KB
testcase_22 AC 115 ms
76,868 KB
testcase_23 AC 118 ms
77,172 KB
testcase_24 AC 114 ms
77,172 KB
testcase_25 AC 115 ms
77,152 KB
testcase_26 AC 116 ms
76,876 KB
testcase_27 AC 408 ms
110,540 KB
testcase_28 AC 376 ms
110,340 KB
testcase_29 AC 422 ms
111,488 KB
testcase_30 AC 380 ms
110,700 KB
testcase_31 AC 407 ms
110,628 KB
testcase_32 AC 322 ms
111,836 KB
testcase_33 AC 304 ms
110,508 KB
testcase_34 AC 329 ms
111,332 KB
testcase_35 AC 331 ms
112,036 KB
testcase_36 AC 320 ms
111,444 KB
testcase_37 AC 297 ms
111,472 KB
testcase_38 AC 325 ms
111,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict
import bisect

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

    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) 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):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


N,A,B = map(int, input().split())
X = list(map(int, input().split())) 
uf = UnionFind(N)
for i in range(N):
    a = bisect.bisect_right(X,X[i]+A-1)
    b = bisect.bisect_right(X,X[i]+B)-1
    for j in reversed(range(a,b+1)):
        if uf.same(i,j):
            break
        else:
            uf.union(i,j)

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