結果

問題 No.2505 matriX cOnstRuction
ユーザー SSRSSSRS
提出日時 2023-10-13 22:44:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,794 bytes
コンパイル時間 2,911 ms
コンパイル使用メモリ 214,408 KB
実行使用メモリ 13,664 KB
最終ジャッジ日時 2023-10-13 22:44:30
合計ジャッジ時間 7,854 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 34 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 34 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 34 ms
4,352 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 12 ms
4,352 KB
testcase_31 AC 12 ms
4,352 KB
testcase_32 AC 13 ms
4,348 KB
testcase_33 AC 12 ms
4,352 KB
testcase_34 AC 17 ms
4,352 KB
testcase_35 AC 16 ms
4,348 KB
testcase_36 AC 16 ms
4,356 KB
testcase_37 AC 18 ms
4,348 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 33 ms
5,244 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
      }
    }
  }
}
0