結果

問題 No.1345 Beautiful BINGO
ユーザー sten_san
提出日時 2021-01-16 14:44:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 526 ms / 2,000 ms
コード長 1,938 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 206,424 KB
最終ジャッジ日時 2025-01-17 23:15:54
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> using lqueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using gqueue = priority_queue<T, vector<T>, less<T>>;

int main() {
    int n, m; cin >> n >> m;
    vvec<int> a(n, vec<int>(n));
    for (auto &e1 : a) for (auto &e2 : e1) cin >> e2;

    int ans = INT_MAX;
    for (int i = 0; i < (1 << (n + 2)); ++i) {
        vvec<bool> used(n, vec<bool>(n, false));

        vec<int> col(n, 0);
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < n; ++k) {
                col[k] += a[j][k];
            }
        }

        int count = 0, cost = 0;
        for (int j = 0; j < n; ++j) {
            if (!(i & (1 << j))) continue;
            for (int k = 0; k < n; ++k) {
                used[j][k] = true;
                cost += a[j][k];
                col[k] -= a[j][k];
            }
            ++count;
        }

        int dia = i >> n;
        if (dia & (1 << 0)) {
            for (int j = 0; j < n; ++j) {
                if (used[j][j]) continue;
                used[j][j] = true;
                cost += a[j][j];
                col[j] -= a[j][j];
            }
            ++count;
        }
        if (dia & (1 << 1)) {
            for (int j = 0; j < n; ++j) {
                int y = j, x = n - j - 1;
                if (used[y][x]) continue;
                used[y][x] = true;
                cost += a[y][x];
                col[x] -= a[y][x];
            }
            ++count;
        }

        sort(begin(col), end(col));
        for (int j = 1; j < n; ++j) {
            col[j] += col[j - 1];
        }

        if (m <= count) {
            ans = min(ans, cost);
        }
        else if (m - count - 1 < n) {
            ans = min(ans, cost + col[m - count - 1]);
        }
    }

    cout << ans << endl;
}

0