結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 emthrmemthrm
提出日時 2023-07-26 19:33:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 305 ms / 2,500 ms
コード長 5,363 bytes
コンパイル時間 1,310 ms
コンパイル使用メモリ 129,944 KB
実行使用メモリ 9,912 KB
最終ジャッジ日時 2023-10-13 18:17:31
合計ジャッジ時間 7,068 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 58 ms
4,348 KB
testcase_02 AC 49 ms
4,352 KB
testcase_03 AC 47 ms
4,352 KB
testcase_04 AC 47 ms
4,352 KB
testcase_05 AC 49 ms
4,348 KB
testcase_06 AC 58 ms
4,352 KB
testcase_07 AC 31 ms
4,348 KB
testcase_08 AC 30 ms
4,348 KB
testcase_09 AC 31 ms
4,348 KB
testcase_10 AC 32 ms
4,348 KB
testcase_11 AC 31 ms
4,352 KB
testcase_12 AC 30 ms
4,352 KB
testcase_13 AC 30 ms
4,352 KB
testcase_14 AC 30 ms
4,348 KB
testcase_15 AC 29 ms
4,352 KB
testcase_16 AC 30 ms
4,348 KB
testcase_17 AC 30 ms
4,348 KB
testcase_18 AC 30 ms
4,348 KB
testcase_19 AC 30 ms
4,348 KB
testcase_20 AC 30 ms
4,348 KB
testcase_21 AC 30 ms
4,348 KB
testcase_22 AC 32 ms
4,352 KB
testcase_23 AC 30 ms
4,348 KB
testcase_24 AC 31 ms
4,352 KB
testcase_25 AC 30 ms
4,348 KB
testcase_26 AC 31 ms
4,352 KB
testcase_27 AC 31 ms
4,348 KB
testcase_28 AC 31 ms
4,348 KB
testcase_29 AC 32 ms
4,348 KB
testcase_30 AC 17 ms
4,348 KB
testcase_31 AC 17 ms
4,348 KB
testcase_32 AC 17 ms
4,348 KB
testcase_33 AC 17 ms
4,352 KB
testcase_34 AC 23 ms
4,348 KB
testcase_35 AC 20 ms
4,352 KB
testcase_36 AC 21 ms
4,352 KB
testcase_37 AC 22 ms
4,352 KB
testcase_38 AC 25 ms
4,352 KB
testcase_39 AC 29 ms
4,352 KB
testcase_40 AC 24 ms
4,348 KB
testcase_41 AC 52 ms
4,376 KB
testcase_42 AC 24 ms
4,352 KB
testcase_43 AC 58 ms
6,984 KB
testcase_44 AC 50 ms
4,536 KB
testcase_45 AC 58 ms
6,996 KB
testcase_46 AC 48 ms
4,524 KB
testcase_47 AC 57 ms
7,052 KB
testcase_48 AC 34 ms
4,352 KB
testcase_49 AC 69 ms
4,928 KB
testcase_50 AC 70 ms
4,788 KB
testcase_51 AC 305 ms
4,348 KB
testcase_52 AC 81 ms
9,248 KB
testcase_53 AC 72 ms
7,228 KB
testcase_54 AC 82 ms
9,912 KB
testcase_55 AC 25 ms
4,348 KB
testcase_56 AC 25 ms
4,348 KB
testcase_57 AC 24 ms
4,348 KB
testcase_58 AC 25 ms
4,348 KB
testcase_59 AC 25 ms
4,348 KB
testcase_60 AC 26 ms
4,352 KB
testcase_61 AC 25 ms
4,348 KB
testcase_62 AC 24 ms
4,348 KB
testcase_63 AC 18 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <iostream>
#include <iterator>
#include <numeric>
#include <tuple>
#include <vector>

int KthBit(const int x, const int k) { return x >> k & 1; }

// <AC>
// できる限り枝刈りを行っている。
template <int D>
int Solve(const std::vector<int>& rc, const std::vector<std::vector<int>>& a) {
  const int n = a.size(), m = a.front().size();
  assert(std::ssize(rc) == n + m);

  // ビットごと 2 グループに分ける
  std::vector group(n + m, std::bitset<D>());
  for (int bit = 0; bit < D; ++bit) {
    const int criteria = KthBit(a.front().front(), bit);
    for (int i = 1; i < n; ++i) {
      const int is_diff = (KthBit(a[i].front(), bit) != criteria ? 1 : 0);
      for (int j = 1; j < m; ++j) {
        if (KthBit(a.front()[j], bit) ^ is_diff != KthBit(a[i][j], bit)) {
          return -1;
        }
      }
      if (is_diff == 1) group[i].set(bit);
    }
    if (criteria == 1) group[n].set(bit);
    for (int j = 1; j < m; ++j) {
      const int is_diff = (KthBit(a.front()[j], bit) != criteria ? 1 : 0);
      for (int i = 1; i < n; ++i) {
        if (KthBit(a[i].front(), bit) ^ is_diff != KthBit(a[i][j], bit)) {
          return -1;
        }
      }
      if (KthBit(a.front()[j], bit) == 1) group[j + n].set(bit);
    }
  }

  const auto Operation = [&rc, &group](
      const int bit, const int choice, const std::vector<int>& or_012,
      const std::vector<int>& or_01, const std::vector<int>& or_12)
          -> std::tuple<bool, int,
                        std::vector<int>, std::vector<int>, std::vector<int>> {
    int cost = 0;
    std::vector<int> next_or_012, next_or_01, next_or_12;
    for (const int i : or_012) {
      if (group[i][bit] == choice) {  // 1
        if (KthBit(rc[i], bit) == 0) {
          return {false, 0,
                  std::vector<int>{}, std::vector<int>{}, std::vector<int>{}};
        }
        ++cost;
        next_or_12.emplace_back(i);
      } else {  // 0
        (KthBit(rc[i], bit) == 1 ? next_or_01 : next_or_012).emplace_back(i);
      }
    }
    for (const int i : or_01) {
      if (group[i][bit] == choice) {
        ++cost;
      } else {
        next_or_01.emplace_back(i);
      }
    }
    for (const int i : or_12) {
      const int bit_of_i = (group[i][bit] == choice ? 1 : 0);
      const int bit_of_rc = KthBit(rc[i], bit);
      if (bit_of_i > bit_of_rc) {
        ++cost;
      } else if (bit_of_i == bit_of_rc) {
        next_or_12.emplace_back(i);
      }
    }
    return {true, cost, next_or_012, next_or_01, next_or_12};
  };

  const int argmin = std::distance(rc.begin(), std::ranges::min_element(rc));
  const int need_to_choose =  // これより上位のビットは自由に選べない
      std::bit_width(static_cast<unsigned int>(rc[argmin]));
  int current_cost = 0;
  std::vector<int> or_012(n + m), or_01, or_12;
  std::iota(or_012.begin(), or_012.end(), 0);
  for (int bit = D - 1; bit >= need_to_choose; --bit) {
    const int choice = group[argmin][bit] ^ 1;
    bool is_valid;
    int added_cost;
    std::tie(is_valid, added_cost, or_012, or_01, or_12) =
        Operation(bit, choice, or_012, or_01, or_12);
    if (!is_valid) return -1;
    current_cost += added_cost;
  }

  int min_cost = (n + m) * 2 + 1;

  // 上位からグループを決定する
  const auto dfs = [Operation, &min_cost](
      auto dfs, const int bit, const int current_cost,
      const std::vector<int>& or_012,
      const std::vector<int>& or_01, const std::vector<int>& or_12) -> void {
    if ((or_012.empty() && or_01.empty() && or_12.empty()) || bit < 0) {
      min_cost = std::min(min_cost, current_cost);
      return;
    }
    std::array<int, 2> next_cost{};
    std::array<std::vector<int>, 2> next_or_012{}, next_or_01{}, next_or_12{};
    for (const int choice : std::array<int, 2>{0, 1}) {
      std::tie(std::ignore, next_cost[choice],
               next_or_012[choice], next_or_01[choice], next_or_12[choice]) =
          Operation(bit, choice, or_012, or_01, or_12);
      next_cost[choice] += current_cost;
    }
    const int earlier =
        (next_cost[0] + next_or_012[0].size() + next_or_01[0].size()
         < next_cost[1] + next_or_012[1].size() + next_or_01[1].size()
         ? 0 : 1);
    for (const int parity : std::array<int, 2>{0, 1}) {
      const int choice = parity ^ earlier;
      if (next_cost[choice] >= min_cost) continue;  // 枝刈り
      dfs(dfs, bit - 1, next_cost[choice],
          next_or_012[choice], next_or_01[choice], next_or_12[choice]);
    }
  };
  dfs(dfs, need_to_choose - 1, current_cost, or_012, or_01, or_12);

  return min_cost;
}

int main() {
  constexpr int kMaxT = 50000, kMaxNM = 50000, kDigits = 30;

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n, m;
    std::cin >> n >> m;
    assert(1 <= n && 1 <= m && n * m <= kMaxNM);
    std::vector<int> rc(n + m);
    for (int i = 0; i < n + m; ++i) {
      std::cin >> rc[i];
      assert(0 <= rc[i] && rc[i] < (1 << kDigits));
    }
    std::vector a(n, std::vector(m, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        std::cin >> a[i][j];
        assert(0 <= a[i][j] && a[i][j] < (1 << kDigits));
      }
    }
    std::cout << Solve<kDigits>(rc, a) << '\n';
  }
  return 0;
}
0