結果

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = (ll)1e9 + 7;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll N, M; cin >> N >> M;
	vector<vector<ll>>A(N, vector<ll>(N));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> A[i][j];
		}
	}
	ll ans = INF;
	for (int i = 0; i < 4; i++) {
		vector<vector<bool>>now(N, vector<bool>(N));
		ll tmp = 0;
		ll tmp2 = 0;
		for (int j = 0; j < 2; j++) {
			if (i & (1 << j)) {
				if (j == 0) {
					for (int k = 0; k < N; k++) {
						if (!now[k][k]) {
							now[k][k] = true;
							tmp += A[k][k];
						}
					}
					tmp2++;
				}
				else {
					for (int k = 0; k < N; k++) {
						if (!now[k][N - k - 1]) {
							now[k][N - k - 1] = true;
							tmp += A[k][N - k - 1];
						}
					}
					tmp2++;
				}
			}
		}
		
		for (int bit = 0; bit < (1 << N); bit++) {
			vector<vector<bool>>b = now;
			ll a = tmp;
			ll cnt = tmp2;
			
			for (int j = 0; j < N; j++) {
				if (bit & (1 << j)) {
					for (int k = 0; k < N; k++) {
						if (!now[j][k]) {
							now[j][k] = true;
							tmp += A[j][k];
						}
					}
					cnt++;
				}
			}
			ll need = M - cnt;
			if (need <= 0) {
				ans = min(ans, tmp);
				
				tmp = a;
				now = b;
				continue;
			}
			if (need > N) {
				tmp = a;
				now = b;
				continue;
			}
			vector<ll>s(N);
			for (int k = 0; k < N; k++) {
				for (int j = 0; j < N; j++) {
					if (!now[j][k]) {
						s[k] += A[j][k];
					}
				}
			}
			sort(s.begin(), s.end());
			for (int j = 0; j < need; j++) {
				tmp += s[j];
			}
			ans = min(ans, tmp);
			
			tmp = a;
			now = b;
		}
	}
	cout << ans << endl;
	return 0;
}
0