結果

問題 No.1170 Never Want to Walk
ユーザー aqua_tenhouaqua_tenhou
提出日時 2020-08-14 22:13:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 869 ms / 2,000 ms
コード長 1,717 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 102,372 KB
最終ジャッジ日時 2024-04-18 22:03:47
合計ジャッジ時間 8,572 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,736 KB
testcase_01 AC 38 ms
53,120 KB
testcase_02 AC 43 ms
53,248 KB
testcase_03 AC 39 ms
52,684 KB
testcase_04 AC 38 ms
52,864 KB
testcase_05 AC 36 ms
52,960 KB
testcase_06 AC 37 ms
52,992 KB
testcase_07 AC 36 ms
52,352 KB
testcase_08 AC 38 ms
52,736 KB
testcase_09 AC 38 ms
52,736 KB
testcase_10 AC 37 ms
52,352 KB
testcase_11 AC 37 ms
52,864 KB
testcase_12 AC 63 ms
69,248 KB
testcase_13 AC 67 ms
73,088 KB
testcase_14 AC 77 ms
76,544 KB
testcase_15 AC 64 ms
70,400 KB
testcase_16 AC 60 ms
68,736 KB
testcase_17 AC 68 ms
73,216 KB
testcase_18 AC 65 ms
70,528 KB
testcase_19 AC 67 ms
71,680 KB
testcase_20 AC 72 ms
74,880 KB
testcase_21 AC 66 ms
70,528 KB
testcase_22 AC 75 ms
74,112 KB
testcase_23 AC 78 ms
76,800 KB
testcase_24 AC 72 ms
73,984 KB
testcase_25 AC 71 ms
73,600 KB
testcase_26 AC 72 ms
73,728 KB
testcase_27 AC 323 ms
102,012 KB
testcase_28 AC 295 ms
102,044 KB
testcase_29 AC 356 ms
102,372 KB
testcase_30 AC 302 ms
101,632 KB
testcase_31 AC 297 ms
102,128 KB
testcase_32 AC 347 ms
102,216 KB
testcase_33 AC 368 ms
101,752 KB
testcase_34 AC 869 ms
101,864 KB
testcase_35 AC 336 ms
102,072 KB
testcase_36 AC 352 ms
101,504 KB
testcase_37 AC 676 ms
101,300 KB
testcase_38 AC 333 ms
101,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#UnionFind
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):
        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())

import bisect

n,a,b = map(int,input().split())
x = list(map(int,input().split()))

uf = UnionFind(n+1)

for i in range(n):
    
    A = bisect.bisect_left(x,x[i]+a)
    B = bisect.bisect_left(x,x[i]+b)
    
    
    for j in range(A,B+1):
        if i == j:
            pass
        
        if A < n and j < n:
            if j != A and a <= abs(x[j]-x[A]) <= b:
                break
        
        
        if j >= n:
            break

        if a <= abs(x[i]-x[j]) <= b:
            uf.union(i,j)
            
for i in range(n):
    print(uf.size(i))
0