結果

問題 No.1170 Never Want to Walk
ユーザー aqua_tenhouaqua_tenhou
提出日時 2020-08-14 21:54:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,661 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 32,844 KB
最終ジャッジ日時 2024-04-18 21:39:12
合計ジャッジ時間 13,686 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,880 KB
testcase_01 AC 32 ms
10,880 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 32 ms
11,008 KB
testcase_04 AC 30 ms
11,008 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 28 ms
11,008 KB
testcase_07 AC 28 ms
11,008 KB
testcase_08 AC 27 ms
11,008 KB
testcase_09 AC 27 ms
11,008 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 27 ms
11,008 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 35 ms
11,136 KB
testcase_23 AC 34 ms
11,136 KB
testcase_24 AC 34 ms
11,136 KB
testcase_25 AC 34 ms
11,008 KB
testcase_26 AC 36 ms
11,008 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 863 ms
32,100 KB
testcase_33 AC 900 ms
31,808 KB
testcase_34 AC 880 ms
32,564 KB
testcase_35 AC 928 ms
32,460 KB
testcase_36 AC 897 ms
32,000 KB
testcase_37 AC 853 ms
32,376 KB
testcase_38 AC 890 ms
32,844 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 j >= n:
            break
        
        if uf.same(i,j):
            break
        else:
            if a <= abs(x[i]-x[j]) <= b:
                uf.union(i,j)
            
for i in range(n):
    print(uf.size(i))
0