結果

問題 No.1170 Never Want to Walk
ユーザー 👑 もぐら 3.0もぐら 3.0
提出日時 2023-11-22 19:06:58
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,702 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 846,176 KB
最終ジャッジ日時 2023-11-22 19:07:10
合計ジャッジ時間 10,935 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,612 KB
testcase_01 AC 40 ms
55,612 KB
testcase_02 AC 39 ms
55,612 KB
testcase_03 AC 39 ms
55,612 KB
testcase_04 AC 39 ms
55,612 KB
testcase_05 AC 40 ms
55,612 KB
testcase_06 AC 41 ms
55,612 KB
testcase_07 AC 41 ms
55,612 KB
testcase_08 AC 40 ms
55,612 KB
testcase_09 AC 40 ms
55,612 KB
testcase_10 AC 40 ms
55,612 KB
testcase_11 AC 40 ms
55,612 KB
testcase_12 AC 70 ms
72,568 KB
testcase_13 AC 95 ms
77,072 KB
testcase_14 AC 92 ms
76,564 KB
testcase_15 AC 71 ms
72,572 KB
testcase_16 AC 80 ms
76,436 KB
testcase_17 AC 94 ms
76,700 KB
testcase_18 AC 69 ms
72,568 KB
testcase_19 AC 83 ms
76,192 KB
testcase_20 AC 112 ms
77,452 KB
testcase_21 AC 73 ms
72,708 KB
testcase_22 AC 214 ms
109,480 KB
testcase_23 AC 128 ms
88,592 KB
testcase_24 AC 233 ms
105,892 KB
testcase_25 AC 222 ms
109,460 KB
testcase_26 AC 172 ms
99,272 KB
testcase_27 AC 821 ms
253,692 KB
testcase_28 AC 852 ms
266,404 KB
testcase_29 AC 788 ms
243,584 KB
testcase_30 AC 827 ms
251,532 KB
testcase_31 AC 785 ms
246,560 KB
testcase_32 MLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
class UnionFind():
    def __init__(self):
        self.parents = dict()                                      
        self.members_set = collections.defaultdict(lambda : set()) 
        self.roots_set = set()                                     
        self.key_ID = dict()                                      
        self.ID_key = dict()                                      
        self.cnt = 0                                              

    def dictf(self,x):
        if x in self.key_ID:
            return self.key_ID[x]
        else:
            self.cnt += 1
            self.key_ID[x] = self.cnt
            self.parents[x] = self.cnt
            self.ID_key[self.cnt] = x
            self.members_set[x].add(x)
            self.roots_set.add(x)
            return self.key_ID[x]

    def find(self, x):
        ID_x = self.dictf(x)
        if self.parents[x] == ID_x:
            return x
        else:
            self.parents[x] = self.key_ID[self.find(self.ID_key[self.parents[x]])]
            return self.ID_key[self.parents[x]]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        if x == y:
            return
        for i in self.members_set[y]:
            self.members_set[x].add(i)
        self.members_set[y] = set()
        self.roots_set.remove(y)
        self.parents[y] = self.key_ID[x]

    def size(self, x):
        return len(self.members_set[self.find(x)])

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

    def members(self, x):
        return self.members_set[self.find(x)]

    def roots(self):
        return self.roots_set

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

    def all_group_members(self):
        return {r: self.members_set[r] for r in self.roots_set}

import bisect
N,A,B = map(int,input().split())
x = list(map(int,input().split()))

# 下準備、駅xからA〜Bの範囲内にある最初の駅と辺を張る。A~Bの範囲内にある駅同士を辺で繋ぐ(n-1本)。
# それらの辺情報をtupleでallに入れる。
all = []
for i in range(N):
    left = bisect.bisect_left(x,x[i]+A)
    right = bisect.bisect_right(x,x[i]+B)
    if right-left==1:
        all.append((i,left))
    elif right-left>=2:
        for j in range(left,right-1):
            all.append((j,j+1))
        all.append((i,j))

#allからつないだ辺情報を取り出して、辺で繋いだ駅をunionしていく
uf_s = UnionFind()
for e in all:
    uf_s.union(e[0], e[1])

#順番にその駅番号が所属するmember数を出力する
for n in range(N):
    print(len(uf_s.members(n)))
0