結果
問題 | No.2505 matriX cOnstRuction |
ユーザー |
![]() |
提出日時 | 2023-10-13 22:44:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,794 bytes |
コンパイル時間 | 2,704 ms |
コンパイル使用メモリ | 209,888 KB |
最終ジャッジ日時 | 2025-02-17 07:25:58 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 13 WA * 51 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ int T; cin >> T; for (int i = 0; i < T; i++){ int n, m; cin >> n >> m; vector<int> R(n); for (int j = 0; j < n; j++){ cin >> R[j]; } vector<int> C(m); for (int j = 0; j < m; j++){ cin >> C[j]; } vector<vector<int>> a(n, vector<int>(m)); for (int j = 0; j < n; j++){ for (int k = 0; k < m; k++){ cin >> a[j][k]; } } bool ok = true; for (int j = 0; j < n - 1; j++){ for (int k = 0; k < m - 1; k++){ if ((a[j][k] ^ a[j][k + 1] ^ a[j + 1][k] ^ a[j + 1][k + 1]) != 0){ ok = false; } } } if (!ok){ cout << -1 << endl; } else { vector<int> A(n + m); for (int j = 0; j < n; j++){ A[j] = R[j]; } for (int j = 0; j < m; j++){ A[n + j] = C[j]; } vector<int> B(n + m); B[0] = 0; for (int j = 1; j < n; j++){ B[j] = B[0] ^ a[0][0] ^ a[j][0]; } for (int j = 0; j < m; j++){ B[n + j] = B[0] ^ a[0][j]; } vector<int> B2 = B; sort(B2.begin(), B2.end()); vector<pair<int, int>> P; P.push_back(make_pair(B2[0], 1)); for (int j = 1; j < n + m; j++){ if (B2[j] == B2[j - 1]){ P.back().second++; } else { P.push_back(make_pair(B2[j], 1)); } } int ans = n + m + 1; auto dfs = [&](auto dfs, vector<int> idx, int L, int R, int d) -> void { if (d == -1 || idx.empty()){ ans = min(ans, n + m); int pL = lower_bound(P.begin(), P.end(), make_pair(L, -1)) - P.begin(); int pR = lower_bound(P.begin(), P.end(), make_pair(R, -1)) - P.begin(); for (int j = pL; j < pR; j++){ ans = min(ans, n + m - P[j].second); } return; } int cnt = idx.size(); bool ok0 = true, ok1 = true; for (int j = 0; j < cnt; j++){ if ((A[idx[j]] >> d & 1) == 0){ if ((B[idx[j]] >> d & 1) == 1){ ok0 = false; } if ((B[idx[j]] >> d & 1) == 0){ ok1 = false; } } } vector<vector<int>> nxt(2); for (int j = 0; j < cnt; j++){ nxt[(A[idx[j]] ^ B[idx[j]]) >> d & 1].push_back(idx[j]); } int m = (L + R) / 2; if (ok0){ dfs(dfs, nxt[0], L, m, d - 1); } if (ok1){ dfs(dfs, nxt[1], m, R, d - 1); } }; vector<int> idx; for (int j = 0; j < n + m; j++){ idx.push_back(j); } dfs(dfs, idx, 0, 1 << 30, 29); if (ans == n + m + 1){ cout << -1 << endl; } else { cout << ans << endl; } } } }