結果
問題 | No.366 ロボットソート |
ユーザー | brthyyjp |
提出日時 | 2022-03-22 09:45:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 2,633 bytes |
コンパイル時間 | 504 ms |
コンパイル使用メモリ | 82,212 KB |
実行使用メモリ | 68,916 KB |
最終ジャッジ日時 | 2024-10-10 00:00:55 |
合計ジャッジ時間 | 2,823 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
54,844 KB |
testcase_01 | AC | 42 ms
54,620 KB |
testcase_02 | AC | 43 ms
56,272 KB |
testcase_03 | AC | 43 ms
55,044 KB |
testcase_04 | AC | 44 ms
55,436 KB |
testcase_05 | AC | 45 ms
54,600 KB |
testcase_06 | AC | 44 ms
54,848 KB |
testcase_07 | AC | 43 ms
55,992 KB |
testcase_08 | AC | 42 ms
55,616 KB |
testcase_09 | AC | 43 ms
54,816 KB |
testcase_10 | AC | 45 ms
55,356 KB |
testcase_11 | AC | 59 ms
67,064 KB |
testcase_12 | AC | 60 ms
68,916 KB |
testcase_13 | AC | 53 ms
63,688 KB |
testcase_14 | AC | 56 ms
66,532 KB |
testcase_15 | AC | 51 ms
62,412 KB |
testcase_16 | AC | 54 ms
63,764 KB |
testcase_17 | AC | 55 ms
65,124 KB |
testcase_18 | AC | 58 ms
67,048 KB |
testcase_19 | AC | 55 ms
63,804 KB |
testcase_20 | AC | 43 ms
54,856 KB |
testcase_21 | AC | 61 ms
67,508 KB |
testcase_22 | AC | 61 ms
68,708 KB |
ソースコード
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)