結果
問題 | No.1345 Beautiful BINGO |
ユーザー | 小野寺健 |
提出日時 | 2021-05-28 09:17:29 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,031 bytes |
コンパイル時間 | 4,613 ms |
コンパイル使用メモリ | 89,400 KB |
実行使用メモリ | 169,536 KB |
最終ジャッジ日時 | 2024-11-07 00:34:41 |
合計ジャッジ時間 | 16,707 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 132 ms
41,240 KB |
testcase_01 | AC | 145 ms
41,444 KB |
testcase_02 | AC | 145 ms
41,428 KB |
testcase_03 | AC | 133 ms
40,316 KB |
testcase_04 | AC | 144 ms
41,496 KB |
testcase_05 | AC | 166 ms
41,812 KB |
testcase_06 | AC | 127 ms
39,988 KB |
testcase_07 | AC | 218 ms
44,780 KB |
testcase_08 | AC | 142 ms
41,316 KB |
testcase_09 | AC | 130 ms
40,372 KB |
testcase_10 | AC | 151 ms
41,708 KB |
testcase_11 | AC | 164 ms
41,996 KB |
testcase_12 | AC | 147 ms
41,404 KB |
testcase_13 | AC | 230 ms
44,612 KB |
testcase_14 | AC | 149 ms
41,556 KB |
testcase_15 | AC | 142 ms
41,176 KB |
testcase_16 | AC | 138 ms
40,240 KB |
testcase_17 | AC | 147 ms
41,724 KB |
testcase_18 | AC | 141 ms
41,096 KB |
testcase_19 | AC | 133 ms
40,232 KB |
testcase_20 | AC | 141 ms
41,436 KB |
testcase_21 | AC | 145 ms
41,472 KB |
testcase_22 | AC | 348 ms
51,576 KB |
testcase_23 | AC | 1,975 ms
169,536 KB |
testcase_24 | AC | 1,584 ms
111,776 KB |
testcase_25 | AC | 472 ms
50,920 KB |
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 | -- | - |
ソースコード
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.HashMap; 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 Row(Row r) { row = r.row; diag = new ArrayList<Integer>(); cnt = r.cnt; col = new int[r.col.length]; size = r.size; } 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]; } } } } 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(); Row[] RowBlt = new Row[1<<(N+2)]; for (int i=0; i < 1 << (N+2); i++) { Row row = null; if ((i & (3 << N)) == 0) { 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); } } } else { row = new Row(RowBlt[i & ((1 << N) - 1)]); 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); RowBlt[i] = row; } List<Integer> Range = new ArrayList<Integer>(); for (int i=0; i < N; i++) { Range.add(i); } int ans = Integer.MAX_VALUE; HashMap<Integer, List<List<Integer>>> combDict = new HashMap<Integer, List<List<Integer>>>(); for (Row row : RowBlt) { if (row.size > M || M - row.size > N) { continue; } else if (row.size == M) { ans = Math.min(ans, row.cnt); } else { if (row.cnt >= ans) { continue; } List<List<Integer>> combination = null; if (combDict.containsKey(M-row.size)) { combination = combDict.get(M-row.size); } else { combination = Combination.make(Range, M-row.size); combDict.put(M-row.size, combination); } for (List<Integer> comb : combination) { int n = 0; for (int c : comb) { n += row.col[c]; if (row.cnt + n >= ans) { break; } } ans = Math.min(ans, row.cnt + n); } } } System.out.println(ans); } }