結果

問題 No.1170 Never Want to Walk
ユーザー 👑 もぐら 3.0もぐら 3.0
提出日時 2023-11-23 19:45:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 954 ms / 2,000 ms
コード長 3,110 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 287,952 KB
最終ジャッジ日時 2023-11-23 19:45:24
合計ジャッジ時間 15,855 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,612 KB
testcase_01 AC 42 ms
55,612 KB
testcase_02 AC 41 ms
55,612 KB
testcase_03 AC 42 ms
55,612 KB
testcase_04 AC 42 ms
55,612 KB
testcase_05 AC 41 ms
55,612 KB
testcase_06 AC 42 ms
55,612 KB
testcase_07 AC 41 ms
55,612 KB
testcase_08 AC 42 ms
55,612 KB
testcase_09 AC 42 ms
55,612 KB
testcase_10 AC 41 ms
55,612 KB
testcase_11 AC 42 ms
55,612 KB
testcase_12 AC 72 ms
72,564 KB
testcase_13 AC 103 ms
76,800 KB
testcase_14 AC 101 ms
76,808 KB
testcase_15 AC 74 ms
72,696 KB
testcase_16 AC 84 ms
76,276 KB
testcase_17 AC 98 ms
76,824 KB
testcase_18 AC 72 ms
72,560 KB
testcase_19 AC 84 ms
76,292 KB
testcase_20 AC 99 ms
76,932 KB
testcase_21 AC 72 ms
72,560 KB
testcase_22 AC 97 ms
76,944 KB
testcase_23 AC 98 ms
76,808 KB
testcase_24 AC 95 ms
76,940 KB
testcase_25 AC 100 ms
77,076 KB
testcase_26 AC 102 ms
76,808 KB
testcase_27 AC 867 ms
236,772 KB
testcase_28 AC 884 ms
241,352 KB
testcase_29 AC 901 ms
233,096 KB
testcase_30 AC 872 ms
240,292 KB
testcase_31 AC 894 ms
238,772 KB
testcase_32 AC 852 ms
251,664 KB
testcase_33 AC 813 ms
256,704 KB
testcase_34 AC 954 ms
267,900 KB
testcase_35 AC 800 ms
248,384 KB
testcase_36 AC 809 ms
253,328 KB
testcase_37 AC 921 ms
287,952 KB
testcase_38 AC 878 ms
261,468 KB
権限があれば一括ダウンロードができます

ソースコード

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の範囲内にある最初の駅と辺を張る。allにtupleで入れておく。
# 駅xからA~Bの距離間に2つ以上駅があったら記録。tempにtupleで入れておく。
all = []
temp = []
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:
        all.append((i,left))
        temp.append((left,right-1))

#tempにある辺について重複区間の処理をする
if temp:
    temp.sort(key=lambda x:x[0])
    new = []
    now = temp[0]
    for i in range(1,len(temp)):
        if now[1]<temp[i][0]:
            new.append(now)
            now = temp[i]
        else:
            now = [now[0],temp[i][1]]
    new.append(now)

    for d in new:
        for f in range(d[0],d[1]):
            all.append((f,f+1))

#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