結果

問題 No.1345 Beautiful BINGO
ユーザー maguromaguro
提出日時 2021-01-15 12:53:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 2,736 bytes
コンパイル時間 2,816 ms
コンパイル使用メモリ 203,568 KB
最終ジャッジ日時 2025-01-17 18:03:20
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000030;
constexpr ll INF= 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;
typedef pair<int,P> PP;

template<class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

template<class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll mod(ll val, ll M) {
    val = val % M;
    if(val < 0) {
        val += M;
    }
    return val;
}

template<typename T>
T RS(T N, T P, T M){
    if(P == 0) {
        return 1;
    }
    if(P < 0) {
        return 0;
    }
    if(P % 2 == 0){
        ll t = RS(N, P/2, M);
        if(M == -1) return t * t;
        return t * t % M;
    }
    if(M == -1) {
        return N * RS(N,P - 1,M);
    }
    return N * RS(N, P-1, M) % M;
}

int N,M;

int solve(vector<vector<int>> vec,int a) {
    int res = Inf;
    for(int bit = 0;bit < (1 << N);bit++) {
        int sum = 0;
        int remain = M - a;
        vector<int> cnt;
        for(int i = 0;i < N;i++) {
            if(bit & (1 << i)) {
                cnt.push_back(i);
            }
        }
        for(auto x:cnt) {
            for(int i = 0;i < N;i++) {
                sum += vec.at(x).at(i);
            }
            remain--;
        }
        vector<int> cnt2(N);
        for(int i = 0;i < N;i++) {
            if(!(bit & (1 << i))) {
                for(int j = 0;j < N;j++) {
                    cnt2.at(j) += vec.at(i).at(j);
                }
            }
        }
        sort(cnt2.begin(),cnt2.end());
        if(remain > N) continue;
        for(int i = 0;i < remain;i++) {
            sum += cnt2.at(i);
        }
        chmin(res,sum);
    }
    return res;
}

int main() {
    cin >> N >> M;
    vector<vector<int>> vec(N,vector<int>(N));
    for(int i = 0;i < N;i++) {
        for(int j = 0;j < N;j++) {
            cin >> vec.at(i).at(j);
        }
    }
    int ret = Inf;
    for(int i = 0;i < 2;i++) {
        for(int j = 0;j < 2;j++) {
            int s = 0;
            vector<vector<int>> cnt = vec;
            if(i) {
                for(int k = 0;k < N;k++) {
                    cnt.at(k).at(k) = 0;
                }
            }
            if(j) {
                for(int k = 0;k < N;k++) {
                    cnt.at(k).at(N - 1 - k) = 0;
                }
            }
            for(int k = 0;k < N;k++) {
                for(int l = 0;l < N;l++) {
                    if(cnt.at(k).at(l) == 0) s += vec.at(k).at(l);
                }
            }
            chmin(ret,solve(cnt,i + j) + s);
        }
    }
    cout << ret << endl;
}
0