結果
問題 |
No.1345 Beautiful BINGO
|
ユーザー |
|
提出日時 | 2021-05-28 10:09:40 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,761 ms / 2,000 ms |
コード長 | 2,248 bytes |
コンパイル時間 | 4,794 ms |
コンパイル使用メモリ | 78,956 KB |
実行使用メモリ | 66,788 KB |
最終ジャッジ日時 | 2024-11-07 01:06:34 |
合計ジャッジ時間 | 46,677 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 61 |
ソースコード
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class No1345 { 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); } }