結果

問題 No.1170 Never Want to Walk
ユーザー stngstng
提出日時 2022-09-18 17:31:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 1,579 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 99,568 KB
最終ジャッジ日時 2023-08-23 18:14:11
合計ジャッジ時間 9,241 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,128 KB
testcase_01 AC 72 ms
70,716 KB
testcase_02 AC 71 ms
70,900 KB
testcase_03 AC 70 ms
70,636 KB
testcase_04 AC 72 ms
70,752 KB
testcase_05 AC 71 ms
70,636 KB
testcase_06 AC 72 ms
71,088 KB
testcase_07 AC 72 ms
70,872 KB
testcase_08 AC 72 ms
70,892 KB
testcase_09 AC 72 ms
70,720 KB
testcase_10 AC 73 ms
71,096 KB
testcase_11 AC 73 ms
70,872 KB
testcase_12 AC 96 ms
75,512 KB
testcase_13 AC 107 ms
76,652 KB
testcase_14 AC 112 ms
77,248 KB
testcase_15 AC 94 ms
75,660 KB
testcase_16 AC 95 ms
75,656 KB
testcase_17 AC 108 ms
77,196 KB
testcase_18 AC 96 ms
75,576 KB
testcase_19 AC 97 ms
75,704 KB
testcase_20 AC 111 ms
76,728 KB
testcase_21 AC 94 ms
75,596 KB
testcase_22 AC 104 ms
77,120 KB
testcase_23 AC 111 ms
77,168 KB
testcase_24 AC 104 ms
77,140 KB
testcase_25 AC 107 ms
76,936 KB
testcase_26 AC 115 ms
77,400 KB
testcase_27 AC 373 ms
99,568 KB
testcase_28 AC 365 ms
98,984 KB
testcase_29 AC 385 ms
99,272 KB
testcase_30 AC 374 ms
99,472 KB
testcase_31 AC 380 ms
99,456 KB
testcase_32 AC 299 ms
99,340 KB
testcase_33 AC 309 ms
99,368 KB
testcase_34 AC 326 ms
98,280 KB
testcase_35 AC 333 ms
98,760 KB
testcase_36 AC 279 ms
99,408 KB
testcase_37 AC 309 ms
99,244 KB
testcase_38 AC 280 ms
98,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1 for _ in range(n+1)]
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n,a,b = map(int,input().split())
x = [int(i) for i in input().split()]

ki = UnionFind(n+1)
idx = [0]*n

for i in range(n):
    ia = bisect.bisect_left(x,x[i]+a)
    ib = bisect.bisect_right(x,x[i]+b)
    #print(i,ia,"srt")
    ix = ia
    for j in range(ib-ia):
        if ix >= ib:
            break
        if not ki.same_check(i,ix):
            ki.union(i,ix)
            idx[ki.find(i)] = max(ix,idx[ki.find(i)])
            ix += 1
        elif ix < idx[ki.find(i)]:
            ix = idx[ki.find(i)]
        else:
            ix += 1
            #print(i,ia+j)
#ki.union(1,5)
li = [0]*n
num = [0]*n

for i in range(n):
    tmp = ki.find(i)
    li[i] = tmp
    num[tmp] += 1
    #print(ki.find(i),"find")

for i in range(n):
    print(num[li[i]])
0