結果

問題 No.1170 Never Want to Walk
ユーザー 👑 KazunKazun
提出日時 2020-08-15 16:49:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 627 ms / 2,000 ms
コード長 3,645 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 107,036 KB
最終ジャッジ日時 2024-04-19 01:30:10
合計ジャッジ時間 8,684 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,248 KB
testcase_01 AC 42 ms
52,992 KB
testcase_02 AC 40 ms
53,120 KB
testcase_03 AC 40 ms
52,864 KB
testcase_04 AC 41 ms
53,248 KB
testcase_05 AC 40 ms
52,992 KB
testcase_06 AC 41 ms
53,248 KB
testcase_07 AC 42 ms
53,376 KB
testcase_08 AC 40 ms
52,608 KB
testcase_09 AC 42 ms
52,864 KB
testcase_10 AC 40 ms
53,120 KB
testcase_11 AC 40 ms
52,736 KB
testcase_12 AC 58 ms
66,816 KB
testcase_13 AC 62 ms
69,760 KB
testcase_14 AC 63 ms
69,656 KB
testcase_15 AC 59 ms
67,584 KB
testcase_16 AC 61 ms
67,712 KB
testcase_17 AC 64 ms
70,272 KB
testcase_18 AC 58 ms
67,072 KB
testcase_19 AC 60 ms
67,840 KB
testcase_20 AC 63 ms
69,760 KB
testcase_21 AC 60 ms
67,584 KB
testcase_22 AC 65 ms
70,016 KB
testcase_23 AC 62 ms
69,504 KB
testcase_24 AC 62 ms
69,248 KB
testcase_25 AC 65 ms
70,016 KB
testcase_26 AC 65 ms
70,656 KB
testcase_27 AC 465 ms
106,216 KB
testcase_28 AC 464 ms
105,968 KB
testcase_29 AC 519 ms
106,764 KB
testcase_30 AC 484 ms
106,836 KB
testcase_31 AC 500 ms
105,984 KB
testcase_32 AC 294 ms
106,000 KB
testcase_33 AC 627 ms
107,008 KB
testcase_34 AC 297 ms
106,740 KB
testcase_35 AC 345 ms
106,728 KB
testcase_36 AC 291 ms
105,896 KB
testcase_37 AC 407 ms
106,396 KB
testcase_38 AC 382 ms
107,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Binary_Search_Small_Count(A,x,equal=False,sort=False):
    """2分探索によって,x未満の要素の個数を調べる.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"未満"がx"以下"になる
    """
    if sort:
        A.sort()

    N=len(A)
    if A[-1]<x:
        return N
    elif x<A[0] or (not equal and x==A[0]):
        return 0

    L,R=0,N
    while R-L>1:
        C=L+(R-L)//2
        if x<A[C] or (not equal and x==A[C]):
            R=C
        else:
            L=C
    return R
#------------------------------------------------
class Union_Find():
    def __init__(self,N):
        """0,1,...,n-1を要素として初期化する.

        n:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        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,yを同一視する.

        x,y:要素
        """
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x]>self.rank[y]:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
        else:
            self.parents[y] += self.parents[x]
            self.parents[x] = y

            if self.rank[x]==self.rank[y]:
                self.rank[y]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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())

class Imos_1:
    def __init__(self,N):
        self.len=N
        self.list=[0]*(N+1)

    def Add(self,F,T,C=1):
        self.list[F]+=C
        self.list[T+1]-=C

    def Cumulative_Sum(self):
        Y=[0]*(self.len)
        S=0
        for i in range(self.len):
            S+=self.list[i]
            Y[i]=S

        return Y
#================================================
N,A,B=map(int,input().split())
X=list(map(int,input().split()))
U=Union_Find(N)
I=Imos_1(2*N)

for i in range(N):
    p=Binary_Search_Small_Count(X,X[i]+A,False)
    q=Binary_Search_Small_Count(X,X[i]+B,True)
    assert 1

    if p<q<N:
        U.union(i,p)
        U.union(i,q-1)
        I.Add(2*p,2*(q-1))
    elif p<q==N:
        U.union(i,p)
        U.union(i,N-1)
        I.Add(2*p,2*(N-1))
    assert 1

J=I.Cumulative_Sum()
assert 1

for k in range(N):
    if J[2*k]!=0 and J[2*k+1]!=0 and J[2*k+2]!=0:
        U.union(k,k+1)

Ans=[0]*N
for i in range(N):
    Ans[i]=U.size(i)

print("\n".join(map(str,Ans)))
0