結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健小野寺健
提出日時 2021-05-27 16:27:22
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,774 bytes
コンパイル時間 4,123 ms
コンパイル使用メモリ 82,568 KB
実行使用メモリ 70,276 KB
最終ジャッジ日時 2024-04-24 03:22:13
合計ジャッジ時間 15,129 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
46,660 KB
testcase_01 AC 117 ms
41,200 KB
testcase_02 AC 111 ms
40,908 KB
testcase_03 AC 102 ms
40,548 KB
testcase_04 AC 107 ms
41,260 KB
testcase_05 AC 158 ms
43,608 KB
testcase_06 AC 111 ms
41,372 KB
testcase_07 AC 292 ms
49,024 KB
testcase_08 AC 109 ms
41,384 KB
testcase_09 AC 109 ms
41,280 KB
testcase_10 AC 122 ms
41,384 KB
testcase_11 AC 130 ms
41,828 KB
testcase_12 AC 109 ms
41,284 KB
testcase_13 AC 302 ms
48,980 KB
testcase_14 AC 105 ms
40,004 KB
testcase_15 AC 109 ms
41,144 KB
testcase_16 AC 128 ms
41,748 KB
testcase_17 AC 111 ms
41,468 KB
testcase_18 AC 110 ms
41,192 KB
testcase_19 AC 99 ms
40,028 KB
testcase_20 AC 108 ms
41,352 KB
testcase_21 AC 109 ms
41,372 KB
testcase_22 AC 160 ms
42,960 KB
testcase_23 AC 267 ms
53,704 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class No1345 {
	
	private static List<List<Integer>> combination(List list, int count) {
		List ret = new ArrayList<List<Integer>>();
		if (count == 1) {
			for (int i=0; i < list.size(); i++) {
				List<Integer> l = new ArrayList<Integer>();
				l.add((int)list.get(i));
				ret.add(l);
			}
			return ret;
		}
		for (int i=0; i < list.size(); i++) {
			if (i + count > list.size()) {
				break;
			}
			Stack stack = new Stack<Integer>();
			stack.push(list.get(i));
			_combination(ret, list, stack, i+1, count);
		}
		return ret;
	}

	private static void _combination(List ret, List list, Stack stack, int index, int count) {
		for (int i=index; i < list.size(); i++) {
			stack.push(list.get(i));
			if (stack.size() == count) {
				ret.add(Arrays.asList(stack.toArray()));
				stack.pop();
				continue;
			}
			_combination(ret, list, stack, i+1, count);
			stack.pop();
		}
	}
	
	private static class Row {
		public List<Integer> row;
		public int cnt;
		public Row() {
			row = new ArrayList<Integer>();
			cnt = 0;
		}
	}
	
	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();
		Row[] RowBlt = new Row[1<<N];
		for (int i=0; i < 1 << N; i++) {
			Row row = new Row();
			for (int j=0; j < N; j++) {
				if ((i & (1 << j)) != 0) {
					row.row.add(j);
					row.cnt += RowSum[j];
				}
			}
			RowBlt[i] = row;
		}
		List<Integer> Range = new ArrayList<Integer>();
		for (int i=0; i < N+2; i++) {
			Range.add(i);
		}
		int ans = Integer.MAX_VALUE;
		for (Row row : RowBlt) {
			if (row.row.size() > M) {
				continue;
			} else if (row.row.size() == M) {
				ans = Math.min(ans, row.cnt);
			} else {
				for (List<Integer> comb : combination(Range, M-row.row.size())) {
					int n = 0;
					for (int c : comb) {
						if (c == N) {
							for (int i=0; i < N; i++) {
								if (!row.row.contains(i) && !comb.contains(i)) {
									n += A[i][i];
								}
							}
						} else if (c == N+1) {
							for (int i=0; i < N; i++) {
								if (!row.row.contains(i) && !comb.contains(N-1-i) && !(comb.contains(N) && i == N-1-i)) {
									n += A[i][N-1-i];
								}
							}							
						} else {
							for (int i=0; i < N; i++) {
								if (!row.row.contains(i)) {
									n += A[i][c];
								}
							}
						}
					}
					ans = Math.min(ans, row.cnt + n);
				}
			}
		}
		System.out.println(ans);
	}
}
0