結果
問題 | No.2505 matriX cOnstRuction |
ユーザー |
|
提出日時 | 2023-02-14 21:04:37 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,334 bytes |
コンパイル時間 | 404 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 319,984 KB |
最終ジャッジ日時 | 2024-09-15 18:44:23 |
合計ジャッジ時間 | 31,138 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 1 WA * 63 |
ソースコード
from itertools import productimport sysfrom typing import List, Tupledef floor_pow2(n: int):x = 1while (x << 1) <= n:x <<= 1return xclass Node:def __init__(self) -> None:self.lch = Noneself.rch = Noneself.weight = 0def left_child_or_create(self) -> 'Node':if self.lch is None:self.lch = Node()return self.lchdef right_child_or_create(self) -> 'Node':if self.rch is None:self.rch = Node()return self.rchL = 30inf = 1 << 30def solve(n: int, m: int, R: List[int], C: List[int], A: List[List[int]]):for i, j in product(range(n), range(m)):if A[0][0] ^ A[0][j] ^ A[i][0] ^ A[i][j]:print(-1)returnroot = Node()# f(X) += W * [X ^ Y > Z]def add_weight(Y: int, Z: int, W: int):YZ = Y ^ Zcur = rootfor bit in reversed(range(L)):if (YZ >> bit) & 1:if not ((Z >> bit) & 1):cur.left_child_or_create().weight += Wcur = cur.right_child_or_create()else:if not ((Z >> bit) & 1):cur.right_child_or_create().weight += Wcur = cur.left_child_or_create()for i in range(n):Y = A[0][0] ^ A[i][0]if R[i]:add_weight(Y, 0, 1)add_weight(Y, R[i], 1)add_weight(Y, 2 * floor_pow2(R[i]) - 1, inf)else:add_weight(Y, 0, inf)for j in range(m):Y = A[0][j]if C[j]:add_weight(Y, 0, 1)add_weight(Y, C[j], 1)add_weight(Y, 2 * floor_pow2(C[j]) - 1, inf)else:add_weight(Y, 0, inf)min_weight = (inf, -1)st : List[Tuple[Node, int, int]] = [(root, 0, 1 << L)]while st:node, l, r = st.pop()weight = node.weightif r - l == 1:min_weight = min(min_weight, (weight, l))continuemid = (l + r) >> 1if node.lch is None:min_weight = min(min_weight, (weight, l))else:node.lch.weight += weightst.append((node.lch, l, mid))if node.rch is None:min_weight = min(min_weight, (weight, mid))else:node.rch.weight += weightst.append((node.rch, mid, r))cost, X = min_weightif cost >= inf:print(-1)returnprint(cost)for i in range(n):s = X ^ A[0][0] ^ A[i][0]if s == 0:continueelif s <= R[i]:print('r', i + 1, s)else:p = floor_pow2(R[i])print('r', i + 1, p)print('r', i + 1, s ^ p)for j in range(m):t = X ^ A[0][j]if t == 0:continueelif t <= C[j]:print('c', j + 1, t)else:p = floor_pow2(C[j])print('c', j + 1, p)print('c', j + 1, t ^ p)input = sys.stdin.readlineT = int(input())for _ in range(T):n, m = map(int, input().split())R = list(map(int, input().split()))C = list(map(int, input().split()))A = [list(map(int, input().split())) for _ in range(n)]solve(n, m, R, C, A)