結果
問題 | No.2884 Pieces on Squares |
ユーザー | Iroha_3856 |
提出日時 | 2024-09-06 21:43:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 669 ms / 2,000 ms |
コード長 | 2,692 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 82,600 KB |
実行使用メモリ | 80,332 KB |
最終ジャッジ日時 | 2024-09-06 21:44:02 |
合計ジャッジ時間 | 11,656 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 184 ms
79,444 KB |
testcase_01 | AC | 188 ms
79,680 KB |
testcase_02 | AC | 119 ms
79,516 KB |
testcase_03 | AC | 145 ms
79,240 KB |
testcase_04 | AC | 228 ms
79,600 KB |
testcase_05 | AC | 213 ms
79,356 KB |
testcase_06 | AC | 191 ms
79,264 KB |
testcase_07 | AC | 198 ms
79,748 KB |
testcase_08 | AC | 195 ms
79,392 KB |
testcase_09 | AC | 257 ms
80,008 KB |
testcase_10 | AC | 85 ms
75,204 KB |
testcase_11 | AC | 95 ms
78,668 KB |
testcase_12 | AC | 547 ms
79,744 KB |
testcase_13 | AC | 522 ms
79,856 KB |
testcase_14 | AC | 143 ms
78,908 KB |
testcase_15 | AC | 165 ms
79,272 KB |
testcase_16 | AC | 175 ms
79,512 KB |
testcase_17 | AC | 133 ms
79,800 KB |
testcase_18 | AC | 196 ms
79,460 KB |
testcase_19 | AC | 213 ms
80,332 KB |
testcase_20 | AC | 248 ms
79,520 KB |
testcase_21 | AC | 207 ms
79,780 KB |
testcase_22 | AC | 237 ms
79,956 KB |
testcase_23 | AC | 661 ms
80,032 KB |
testcase_24 | AC | 666 ms
80,104 KB |
testcase_25 | AC | 669 ms
79,976 KB |
testcase_26 | AC | 193 ms
79,904 KB |
testcase_27 | AC | 185 ms
79,456 KB |
testcase_28 | AC | 193 ms
79,800 KB |
testcase_29 | AC | 563 ms
80,164 KB |
testcase_30 | AC | 171 ms
78,588 KB |
testcase_31 | AC | 176 ms
79,172 KB |
testcase_32 | AC | 101 ms
78,748 KB |
testcase_33 | AC | 104 ms
78,448 KB |
testcase_34 | AC | 102 ms
78,488 KB |
testcase_35 | AC | 62 ms
68,856 KB |
testcase_36 | AC | 63 ms
67,972 KB |
testcase_37 | AC | 63 ms
68,016 KB |
testcase_38 | AC | 62 ms
67,420 KB |
testcase_39 | AC | 63 ms
67,784 KB |
testcase_40 | AC | 62 ms
67,748 KB |
testcase_41 | AC | 63 ms
67,628 KB |
testcase_42 | AC | 63 ms
68,652 KB |
testcase_43 | AC | 139 ms
78,628 KB |
testcase_44 | AC | 103 ms
78,196 KB |
testcase_45 | AC | 64 ms
68,316 KB |
testcase_46 | AC | 62 ms
68,812 KB |
testcase_47 | AC | 63 ms
68,840 KB |
ソースコード
#Union-Findライブラリ(ac-library python) #https://github.com/not522/ac-library-python/blob/master/atcoder/dsu.py import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= b < self._n x = self.leader(a) y = self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) #ライブラリ終わり H, W, N = map(int, input().split()) d = DSU(H+W); for i in range(N): a, b = map(int, input().split()) a -= 1 b -= 1 d.merge(a, b+H) res = d.groups() K = len(res) dpmin = [10**9 for i in range(H+1)] dpmax = [-10**9 for i in range(H+1)] dpmin[0] = 0 dpmax[0] = 0 for i in range(K): rcnt, ccnt = 0, 0 for v in res[i]: if v < H: rcnt+=1 else: ccnt+=1 for j in range(H, -1, -1): if dpmin[j] != 10**9: dpmin[j+rcnt] = min(dpmin[j+rcnt], dpmin[j] + ccnt) dpmax[j+rcnt] = max(dpmax[j+rcnt], dpmax[j] + ccnt) ans = 0 for i in range(H+1): if dpmin[i] != 10**9: ans = max(ans, i*(W-dpmin[i])+(H-i)*dpmin[i]) ans = max(ans, i*(W-dpmax[i])+(H-i)*dpmax[i]) print(ans)