結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-02-14 19:53:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,788 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 102,184 KB
実行使用メモリ 91,224 KB
最終ジャッジ日時 2023-10-13 22:54:28
合計ジャッジ時間 11,509 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
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 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
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 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
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 584 ms
91,224 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 <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>

int floor_pow2(int n) {
    int x = 1;
    while ((x << 1) <= n) x <<= 1;
    return x;
}

void println() {
    std::cout << '\n';
}
template <typename Head, typename ...Tails>
void println(Head&& head, Tails &&...tails) {
    std::cout << std::forward<Head>(head);
    if constexpr (sizeof...(Tails)) {
        std::cout << ' ';
    }
    println(std::forward<Tails>(tails)...);
}

using Weight = long long;

constexpr int L = 30;

constexpr Weight inf = std::numeric_limits<int>::max();

void solve(int n, int m, const std::vector<int>& r, const std::vector<int>& c, const std::vector<std::vector<int>>& a) {
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
        if (a[0][0] ^ a[0][j] ^ a[i][0] ^ a[i][j]) {
            println(-1);
            return;
        }
    }

    // (l, r, w): add w to x in [l, r)
    std::vector<std::tuple<int, int, Weight>> costs;

    // add f(X) = W * [X ^ Y > Z]
    auto add_cost = [&](int Y, int Z, Weight W) {
        const int YZ = Y ^ Z;

        std::vector<std::tuple<int, int, Weight>> costs_l, costs_r;
        int l = 0, r = 1 << L;
        for (int bit = L - 1; bit >= 0; --bit) {
            int mid = (l + r) >> 1;
            if ((YZ >> bit) & 1) {
                if (not ((Z >> bit) & 1)) {
                    if (costs_l.size() and std::get<1>(costs_l.back()) == l) {
                        std::get<1>(costs_l.back()) = mid;
                    } else {
                        costs_l.emplace_back(l, mid, W);
                    }
                }
                l = mid;
            } else {
                if (not ((Z >> bit) & 1)) {
                    if (costs_r.size() and std::get<1>(costs_r.back()) == r) {
                        std::get<1>(costs_r.back()) = mid;
                    } else {
                        costs_r.emplace_back(mid, r, W);
                    }
                }
                r = mid;
            }
        }
        std::move(costs_l.begin(), costs_l.end(), std::back_inserter(costs));
        std::move(costs_r.begin(), costs_r.end(), std::back_inserter(costs));
    };
    for (int i = 0; i < n; ++i) {
        int Y = a[0][0] ^ a[i][0];
        if (r[i]) {
            add_cost(Y, 0, 1);
            add_cost(Y, r[i], 1);
            add_cost(Y, 2 * floor_pow2(r[i]) - 1, inf);
        } else {
            add_cost(Y, 0, inf);
        }
    }
    for (int j = 0; j < m; ++j) {
        int Y = a[0][j];
        if (c[j]) {
            add_cost(Y, 0, 1);
            add_cost(Y, c[j], 1);
            add_cost(Y, 2 * floor_pow2(c[j]) - 1, inf);
        } else {
            add_cost(Y, 0, inf);
        }
    }
    std::vector<int> xs { 0, 1 << L };
    for (auto [l, r, w] : costs) {
        xs.push_back(l);
        xs.push_back(r);
    }
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    const int k = xs.size();
    std::vector<Weight> imos(k);
    for (auto [l, r, w] : costs) {
        imos[std::lower_bound(xs.begin(), xs.end(), l) - xs.begin()] += w;
        imos[std::lower_bound(xs.begin(), xs.end(), r) - xs.begin()] -= w;
    }
    for (int i = 1; i < k; ++i) {
        imos[i] += imos[i - 1];
    }

    std::pair<Weight, int> min_cost{ inf, 0 };
    for (int i = 0; i < k - 1; ++i) {
        min_cost = std::min(min_cost, std::make_pair(imos[i], xs[i]));
    }

    auto [cost, X] = min_cost;

    if (cost >= inf) {
        println(-1);
        return;
    }

    println(cost);
    std::vector<std::tuple<char, int, int>> ops;
    for (int i = 0; i < n; ++i) {
        int s = X ^ a[0][0] ^ a[i][0];
        if (s == 0) {
            continue;
        } else if (s <= r[i]) {
            println('r', i + 1, s);
        } else {
            int p = floor_pow2(r[i]);
            println('r', i + 1, p);
            println('r', i + 1, s ^ p);
        }
    }
    for (int j = 0; j < m; ++j) {
        int t = X ^ a[0][j];
        if (t == 0) {
            continue;
        } else if (t <= c[j]) {
            println('c', j + 1, t);
        } else {
            int p = floor_pow2(c[j]);
            println('c', j + 1, p);
            println('c', j + 1, t ^ p);
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t-- > 0) {
        int n, m;
        std::cin >> n >> m;

        std::vector<int> r(n), c(m);
        for (auto& e : r) std::cin >> e;
        for (auto& e : c) std::cin >> e;

        std::vector a(n, std::vector<int>(m));
        for (auto& row : a) for (auto& e : row) {
            std::cin >> e;
        }

        solve(n, m, r, c, a);
    }

    return 0;
}
0