結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健小野寺健
提出日時 2021-05-28 10:09:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,535 ms / 2,000 ms
コード長 2,248 bytes
コンパイル時間 3,738 ms
コンパイル使用メモリ 78,648 KB
実行使用メモリ 58,144 KB
最終ジャッジ日時 2024-04-24 16:26:51
合計ジャッジ時間 38,930 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
41,236 KB
testcase_01 AC 109 ms
40,904 KB
testcase_02 AC 102 ms
40,372 KB
testcase_03 AC 107 ms
41,264 KB
testcase_04 AC 102 ms
40,152 KB
testcase_05 AC 136 ms
41,484 KB
testcase_06 AC 110 ms
41,396 KB
testcase_07 AC 150 ms
42,720 KB
testcase_08 AC 109 ms
41,448 KB
testcase_09 AC 108 ms
40,952 KB
testcase_10 AC 113 ms
40,520 KB
testcase_11 AC 115 ms
40,428 KB
testcase_12 AC 116 ms
41,208 KB
testcase_13 AC 156 ms
42,604 KB
testcase_14 AC 107 ms
40,516 KB
testcase_15 AC 110 ms
41,084 KB
testcase_16 AC 118 ms
41,324 KB
testcase_17 AC 115 ms
40,824 KB
testcase_18 AC 112 ms
41,336 KB
testcase_19 AC 115 ms
41,300 KB
testcase_20 AC 110 ms
40,944 KB
testcase_21 AC 121 ms
41,260 KB
testcase_22 AC 298 ms
48,308 KB
testcase_23 AC 1,408 ms
58,144 KB
testcase_24 AC 1,098 ms
52,248 KB
testcase_25 AC 247 ms
46,128 KB
testcase_26 AC 828 ms
55,552 KB
testcase_27 AC 1,493 ms
52,088 KB
testcase_28 AC 226 ms
46,168 KB
testcase_29 AC 628 ms
52,120 KB
testcase_30 AC 471 ms
51,288 KB
testcase_31 AC 291 ms
47,472 KB
testcase_32 AC 159 ms
43,932 KB
testcase_33 AC 1,433 ms
52,140 KB
testcase_34 AC 205 ms
45,856 KB
testcase_35 AC 198 ms
46,076 KB
testcase_36 AC 1,406 ms
54,836 KB
testcase_37 AC 202 ms
45,784 KB
testcase_38 AC 760 ms
55,808 KB
testcase_39 AC 1,404 ms
51,096 KB
testcase_40 AC 1,014 ms
52,076 KB
testcase_41 AC 1,061 ms
51,864 KB
testcase_42 AC 101 ms
40,124 KB
testcase_43 AC 110 ms
41,068 KB
testcase_44 AC 113 ms
40,940 KB
testcase_45 AC 118 ms
41,012 KB
testcase_46 AC 115 ms
41,064 KB
testcase_47 AC 115 ms
41,180 KB
testcase_48 AC 102 ms
39,848 KB
testcase_49 AC 112 ms
41,072 KB
testcase_50 AC 104 ms
40,664 KB
testcase_51 AC 103 ms
39,820 KB
testcase_52 AC 1,535 ms
55,292 KB
testcase_53 AC 1,409 ms
52,856 KB
testcase_54 AC 1,398 ms
55,320 KB
testcase_55 AC 1,364 ms
55,208 KB
testcase_56 AC 1,463 ms
52,740 KB
testcase_57 AC 1,511 ms
50,868 KB
testcase_58 AC 1,370 ms
52,788 KB
testcase_59 AC 1,468 ms
51,848 KB
testcase_60 AC 1,399 ms
54,748 KB
testcase_61 AC 1,326 ms
53,060 KB
testcase_62 AC 1,360 ms
54,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class No1345 {
	
	private static class Row {
		public List<Integer> row;
		public List<Integer> diag;
		public int cnt;
		public int[] col;
		public int size;
		public Row(int n) {
			row = new ArrayList<Integer>();
			diag = new ArrayList<Integer>();
			cnt = 0;
			col = new int[n];
			size = 0;
		}
		public void SetCol(int[][] A) {
			for (int i=0; i < col.length; i++) {
				for (int j : row) {
					if (!ContainDiag(j, i)) {
						col[i] += A[j][i];
					}
				}
			}
			Arrays.sort(col);
			for (int i=0; i < col.length-1; i++) {
				col[i+1] += col[i];
			}
		}
		public void SetDiag(int row, int col) {
			int i = (row << 16) + col;
			if (!diag.contains(i)) {
				diag.add(i);
			}
		}
		public boolean ContainDiag(int row, int col) {
			int i = (row << 16) + col;
			return diag.contains(i);
		}
	}
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int M = scan.nextInt();
		int[] RowSum = new int[N];
		int[][] A = new int[N][N];
		for (int i=0; i < N; i++) {
			int sum = 0;
			for (int j=0; j < N; j++) {
				int a = scan.nextInt();
				sum += a;
				A[i][j] = a;
			}
			RowSum[i] = sum;
		}
		scan.close();
		int ans = Integer.MAX_VALUE;
		for (int i=0; i < 1 << (N+2); i++) {
			Row row = new Row(N);
			for (int j=0; j < N; j++) {
				if ((i & (1 << j)) != 0) {
					row.cnt += RowSum[j];
					row.size++;
				} else {
					row.row.add(j);
				}
			}
			if ((i & (3 << N)) != 0) {
				for (int j = 0; j < N; j++) {
					if (row.row.contains(j)) {
						if ((i & (1 << N)) != 0) {
							row.cnt += A[j][j];
							row.SetDiag(j, j);
						}
						if ((i & (1 << (N+1))) != 0) {
							if (!row.ContainDiag(j, N-1-j)) {
								row.cnt += A[j][N-1-j];
								row.SetDiag(j, N-1-j);
							}
						}
					}
				}
				if ((i & (1 << N)) != 0) {
					row.size++;
				}
				if ((i & (1 << (N+1))) != 0) {
					row.size++;
				}
			}
			row.SetCol(A);
			if (row.size == M) {
				ans = Math.min(ans, row.cnt);
			} else if (row.size < M && M - row.size <= N) {
				ans = Math.min(ans, row.cnt + row.col[M-row.size-1]);
			}
		}
		System.out.println(ans);
	}
}
0