結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健小野寺健
提出日時 2021-05-28 09:51:41
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,498 bytes
コンパイル時間 5,653 ms
コンパイル使用メモリ 95,584 KB
実行使用メモリ 205,028 KB
最終ジャッジ日時 2024-04-24 16:17:34
合計ジャッジ時間 49,171 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
41,328 KB
testcase_01 AC 134 ms
41,292 KB
testcase_02 AC 123 ms
41,236 KB
testcase_03 AC 126 ms
41,164 KB
testcase_04 AC 123 ms
41,100 KB
testcase_05 AC 141 ms
41,400 KB
testcase_06 AC 124 ms
41,180 KB
testcase_07 AC 170 ms
42,692 KB
testcase_08 AC 128 ms
41,204 KB
testcase_09 AC 129 ms
41,444 KB
testcase_10 AC 126 ms
41,188 KB
testcase_11 AC 133 ms
41,256 KB
testcase_12 AC 133 ms
41,536 KB
testcase_13 AC 168 ms
42,692 KB
testcase_14 AC 122 ms
40,624 KB
testcase_15 AC 125 ms
41,256 KB
testcase_16 AC 121 ms
40,444 KB
testcase_17 AC 119 ms
40,848 KB
testcase_18 AC 133 ms
40,908 KB
testcase_19 AC 134 ms
41,328 KB
testcase_20 AC 121 ms
40,128 KB
testcase_21 AC 123 ms
41,000 KB
testcase_22 AC 334 ms
48,148 KB
testcase_23 AC 1,408 ms
55,488 KB
testcase_24 AC 1,100 ms
54,936 KB
testcase_25 AC 277 ms
48,116 KB
testcase_26 AC 854 ms
85,912 KB
testcase_27 TLE -
testcase_28 AC 290 ms
45,880 KB
testcase_29 AC 939 ms
87,704 KB
testcase_30 AC 522 ms
48,904 KB
testcase_31 AC 332 ms
47,684 KB
testcase_32 AC 181 ms
44,080 KB
testcase_33 TLE -
testcase_34 AC 225 ms
45,260 KB
testcase_35 AC 217 ms
45,472 KB
testcase_36 AC 1,548 ms
71,296 KB
testcase_37 AC 212 ms
45,476 KB
testcase_38 AC 916 ms
83,116 KB
testcase_39 TLE -
testcase_40 AC 1,237 ms
116,940 KB
testcase_41 AC 983 ms
69,336 KB
testcase_42 AC 112 ms
40,348 KB
testcase_43 AC 114 ms
40,720 KB
testcase_44 AC 119 ms
41,464 KB
testcase_45 AC 112 ms
40,464 KB
testcase_46 AC 125 ms
41,300 KB
testcase_47 AC 124 ms
41,404 KB
testcase_48 AC 125 ms
40,240 KB
testcase_49 AC 122 ms
41,316 KB
testcase_50 AC 116 ms
41,172 KB
testcase_51 AC 119 ms
41,176 KB
testcase_52 AC 1,470 ms
55,488 KB
testcase_53 TLE -
testcase_54 AC 1,619 ms
71,520 KB
testcase_55 AC 1,734 ms
114,036 KB
testcase_56 AC 1,419 ms
54,088 KB
testcase_57 TLE -
testcase_58 TLE -
testcase_59 TLE -
testcase_60 AC 1,633 ms
87,056 KB
testcase_61 TLE -
testcase_62 AC 1,449 ms
52,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
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) {
	        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];
					}
				}
			}
			Arrays.sort(col);
		}
		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;
		List<Row> RowBlt = new ArrayList<Row>();
		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);
			}
			if (row.size < M && M - row.size <= N) {
				RowBlt.add(row);
			}
		}
		List<Integer> Range = new ArrayList<Integer>();
		for (int i=0; i < N; i++) {
			Range.add(i);
		}
		for (Row row : RowBlt) {
			if (row.cnt >= ans) {
				continue;
			}
			int n = 0;
			for (int i=0; i < M-row.size; i++) {
				n += row.col[i];
			}
			ans = Math.min(ans, row.cnt + n);
		}
		System.out.println(ans);
	}
}
0