結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健小野寺健
提出日時 2021-05-27 16:40:20
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,203 bytes
コンパイル時間 5,100 ms
コンパイル使用メモリ 89,172 KB
実行使用メモリ 54,412 KB
最終ジャッジ日時 2024-04-24 03:27:31
合計ジャッジ時間 14,640 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
41,744 KB
testcase_01 AC 155 ms
41,628 KB
testcase_02 AC 144 ms
41,600 KB
testcase_03 AC 133 ms
40,324 KB
testcase_04 AC 143 ms
41,560 KB
testcase_05 AC 255 ms
46,384 KB
testcase_06 AC 133 ms
40,332 KB
testcase_07 AC 582 ms
52,692 KB
testcase_08 AC 143 ms
41,120 KB
testcase_09 AC 145 ms
41,232 KB
testcase_10 AC 190 ms
42,008 KB
testcase_11 AC 203 ms
42,416 KB
testcase_12 AC 150 ms
41,496 KB
testcase_13 AC 587 ms
52,556 KB
testcase_14 AC 165 ms
41,868 KB
testcase_15 AC 143 ms
41,252 KB
testcase_16 AC 194 ms
42,484 KB
testcase_17 AC 154 ms
41,640 KB
testcase_18 AC 146 ms
41,088 KB
testcase_19 AC 156 ms
41,496 KB
testcase_20 AC 143 ms
41,444 KB
testcase_21 AC 147 ms
41,232 KB
testcase_22 AC 167 ms
42,072 KB
testcase_23 AC 245 ms
52,360 KB
testcase_24 AC 447 ms
54,412 KB
testcase_25 TLE -
testcase_26 -- -
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.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) {
	        // 5C6みたいなのは空
	        // 0C5も空
	        // 5C0も空
	        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<>();
	        // 5C3だったら、添字0, 1, 2だけ考えたらいい
	        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 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 || M - row.row.size() > N+2) {
				continue;
			} else if (row.row.size() == M) {
				ans = Math.min(ans, row.cnt);
			} else {
				for (List<Integer> comb : Combination.make(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