結果

問題 No.366 ロボットソート
ユーザー brthyyjpbrthyyjp
提出日時 2022-03-22 09:45:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,633 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 66,944 KB
最終ジャッジ日時 2024-04-18 06:49:32
合計ジャッジ時間 3,029 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
54,144 KB
testcase_01 AC 51 ms
54,144 KB
testcase_02 AC 51 ms
54,400 KB
testcase_03 AC 51 ms
54,400 KB
testcase_04 AC 53 ms
54,016 KB
testcase_05 AC 52 ms
54,784 KB
testcase_06 AC 52 ms
54,400 KB
testcase_07 AC 52 ms
54,400 KB
testcase_08 AC 52 ms
54,144 KB
testcase_09 AC 51 ms
53,888 KB
testcase_10 AC 55 ms
54,400 KB
testcase_11 AC 69 ms
66,048 KB
testcase_12 AC 72 ms
66,816 KB
testcase_13 AC 64 ms
62,976 KB
testcase_14 AC 67 ms
65,920 KB
testcase_15 AC 62 ms
62,080 KB
testcase_16 AC 63 ms
62,592 KB
testcase_17 AC 65 ms
63,360 KB
testcase_18 AC 70 ms
66,304 KB
testcase_19 AC 66 ms
63,232 KB
testcase_20 AC 51 ms
53,888 KB
testcase_21 AC 73 ms
66,688 KB
testcase_22 AC 72 ms
66,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [0]*n

    def Find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.Find(self.par[x])
            return self.par[x]

    def Unite(self, x, y):
        x = self.Find(x)
        y = self.Find(y)

        if x != y:
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x]
                self.par[x] = y
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1

    def Same(self, x, y):
        return self.Find(x) == self.Find(y)

    def Size(self, x):
        return -self.par[self.Find(x)]

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

from collections import defaultdict

n, k = map(int, input().split())
A = list(map(int, input().split()))
uf = UnionFind(n)
for i in range(n):
    if i+k <= n-1:
        uf.Unite(i, i+k)
X = set(A)
X = list(X)
X.sort()
toid = {}
for i, x in enumerate(X):
    toid[x] = i
A = [toid[a] for a in A]
N = len(X)
I = defaultdict(lambda: [])
B = defaultdict(lambda: [])
for i, a in enumerate(A):
    p = uf.Find(i)
    I[p].append(i)
    B[p].append(a)

ans = 0
C = [-1]*n
for p in I.keys():
    L = B[p]
    bit = BIT(N)
    for a in L:
        ans += bit.sum(a+1, bit.n)
        bit.add(a, 1)
    L.sort()
    for i, b in zip(I[p], L):
        C[i] = b
if C == sorted(A):
    print(ans)
else:
    print(-1)
0