結果
問題 |
No.1345 Beautiful BINGO
|
ユーザー |
|
提出日時 | 2021-05-27 16:40:20 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,203 bytes |
コンパイル時間 | 4,617 ms |
コンパイル使用メモリ | 89,304 KB |
実行使用メモリ | 89,428 KB |
最終ジャッジ日時 | 2024-11-06 10:18:05 |
合計ジャッジ時間 | 14,083 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 TLE * 1 -- * 37 |
ソースコード
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); } }