結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健小野寺健
提出日時 2021-05-28 10:06:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,755 ms / 2,000 ms
コード長 3,128 bytes
コンパイル時間 4,448 ms
コンパイル使用メモリ 95,468 KB
実行使用メモリ 58,156 KB
最終ジャッジ日時 2024-04-24 16:24:32
合計ジャッジ時間 45,965 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
41,400 KB
testcase_01 AC 119 ms
40,252 KB
testcase_02 AC 133 ms
41,616 KB
testcase_03 AC 127 ms
41,280 KB
testcase_04 AC 121 ms
40,420 KB
testcase_05 AC 142 ms
40,804 KB
testcase_06 AC 130 ms
41,676 KB
testcase_07 AC 176 ms
42,740 KB
testcase_08 AC 131 ms
41,288 KB
testcase_09 AC 121 ms
40,080 KB
testcase_10 AC 127 ms
40,300 KB
testcase_11 AC 152 ms
41,544 KB
testcase_12 AC 118 ms
40,072 KB
testcase_13 AC 179 ms
42,688 KB
testcase_14 AC 136 ms
41,512 KB
testcase_15 AC 129 ms
41,192 KB
testcase_16 AC 127 ms
40,204 KB
testcase_17 AC 133 ms
41,440 KB
testcase_18 AC 134 ms
41,268 KB
testcase_19 AC 136 ms
41,488 KB
testcase_20 AC 130 ms
41,624 KB
testcase_21 AC 121 ms
39,824 KB
testcase_22 AC 345 ms
47,460 KB
testcase_23 AC 1,717 ms
58,156 KB
testcase_24 AC 1,211 ms
52,348 KB
testcase_25 AC 271 ms
46,412 KB
testcase_26 AC 786 ms
51,660 KB
testcase_27 AC 1,703 ms
52,140 KB
testcase_28 AC 272 ms
46,256 KB
testcase_29 AC 800 ms
54,108 KB
testcase_30 AC 477 ms
51,128 KB
testcase_31 AC 351 ms
48,476 KB
testcase_32 AC 187 ms
44,276 KB
testcase_33 AC 1,671 ms
52,004 KB
testcase_34 AC 236 ms
45,928 KB
testcase_35 AC 234 ms
45,688 KB
testcase_36 AC 1,549 ms
55,028 KB
testcase_37 AC 233 ms
46,148 KB
testcase_38 AC 731 ms
51,160 KB
testcase_39 AC 1,636 ms
51,168 KB
testcase_40 AC 1,215 ms
52,504 KB
testcase_41 AC 1,196 ms
52,180 KB
testcase_42 AC 127 ms
41,016 KB
testcase_43 AC 126 ms
41,264 KB
testcase_44 AC 130 ms
41,396 KB
testcase_45 AC 132 ms
41,396 KB
testcase_46 AC 116 ms
40,104 KB
testcase_47 AC 114 ms
40,208 KB
testcase_48 AC 128 ms
41,424 KB
testcase_49 AC 120 ms
40,856 KB
testcase_50 AC 120 ms
40,248 KB
testcase_51 AC 131 ms
41,696 KB
testcase_52 AC 1,681 ms
54,800 KB
testcase_53 AC 1,554 ms
52,156 KB
testcase_54 AC 1,599 ms
55,392 KB
testcase_55 AC 1,634 ms
55,204 KB
testcase_56 AC 1,605 ms
53,008 KB
testcase_57 AC 1,755 ms
55,040 KB
testcase_58 AC 1,682 ms
55,208 KB
testcase_59 AC 1,674 ms
51,504 KB
testcase_60 AC 1,545 ms
50,976 KB
testcase_61 AC 1,633 ms
55,560 KB
testcase_62 AC 1,567 ms
54,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class No1345 {
	
	public static class Combination {
	    public static List<List<Integer>> make (List<Integer> candidate, int r) {
	        if (candidate.size() < r || candidate.size() <= 0 || r <= 0) {
	            List<List<Integer>> empty = new ArrayList<>();
	            empty.add(new ArrayList<>());
	            return empty;
	        }

	        List<List<Integer>> combination = new ArrayList<>();
	        for (int i = 0; i <= candidate.size() - r; i++) {
	            Integer picked = candidate.get(i);
	            List<Integer> rest = new ArrayList<>(candidate);
	            rest.subList(0, i + 1).clear();
	            combination.addAll(make(rest, r - 1).stream().map(list -> {
	                list.add(0, picked);
	                return list;
	            }).collect(Collectors.toList()));
	        }
	        return combination;
	    }
	}
	
	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