結果
問題 | No.5013 セクスタプル (open) |
ユーザー | EvbCFfp1XB |
提出日時 | 2022-12-29 21:53:21 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,908 ms / 2,000 ms |
コード長 | 20,263 bytes |
コンパイル時間 | 2,116 ms |
実行使用メモリ | 57,828 KB |
スコア | 18,369 |
最終ジャッジ日時 | 2022-12-29 21:56:42 |
合計ジャッジ時間 | 197,200 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,864 ms
50,712 KB |
testcase_01 | AC | 1,868 ms
50,236 KB |
testcase_02 | AC | 1,877 ms
52,248 KB |
testcase_03 | AC | 1,885 ms
50,440 KB |
testcase_04 | AC | 1,880 ms
50,556 KB |
testcase_05 | AC | 1,869 ms
50,504 KB |
testcase_06 | AC | 1,885 ms
50,992 KB |
testcase_07 | AC | 1,879 ms
50,364 KB |
testcase_08 | AC | 1,873 ms
50,376 KB |
testcase_09 | AC | 1,872 ms
51,956 KB |
testcase_10 | AC | 1,887 ms
50,776 KB |
testcase_11 | AC | 1,878 ms
52,376 KB |
testcase_12 | AC | 1,902 ms
51,068 KB |
testcase_13 | AC | 1,882 ms
50,256 KB |
testcase_14 | AC | 1,873 ms
50,484 KB |
testcase_15 | AC | 1,885 ms
51,064 KB |
testcase_16 | AC | 1,879 ms
50,376 KB |
testcase_17 | AC | 1,872 ms
52,404 KB |
testcase_18 | AC | 1,871 ms
50,396 KB |
testcase_19 | AC | 1,880 ms
50,468 KB |
testcase_20 | AC | 1,872 ms
52,632 KB |
testcase_21 | AC | 1,867 ms
50,276 KB |
testcase_22 | AC | 1,875 ms
50,592 KB |
testcase_23 | AC | 1,871 ms
50,508 KB |
testcase_24 | AC | 1,887 ms
50,568 KB |
testcase_25 | AC | 1,904 ms
50,572 KB |
testcase_26 | AC | 1,892 ms
50,628 KB |
testcase_27 | AC | 1,907 ms
50,564 KB |
testcase_28 | AC | 1,885 ms
52,668 KB |
testcase_29 | AC | 1,884 ms
49,728 KB |
testcase_30 | AC | 1,878 ms
52,400 KB |
testcase_31 | AC | 1,890 ms
52,248 KB |
testcase_32 | AC | 1,879 ms
51,768 KB |
testcase_33 | AC | 1,880 ms
50,560 KB |
testcase_34 | AC | 1,887 ms
52,424 KB |
testcase_35 | AC | 1,898 ms
50,612 KB |
testcase_36 | AC | 1,875 ms
50,240 KB |
testcase_37 | AC | 1,875 ms
51,848 KB |
testcase_38 | AC | 1,883 ms
52,004 KB |
testcase_39 | AC | 1,875 ms
51,248 KB |
testcase_40 | AC | 1,883 ms
52,092 KB |
testcase_41 | AC | 1,886 ms
52,740 KB |
testcase_42 | AC | 1,885 ms
52,676 KB |
testcase_43 | AC | 1,888 ms
50,680 KB |
testcase_44 | AC | 1,884 ms
50,652 KB |
testcase_45 | AC | 1,885 ms
52,732 KB |
testcase_46 | AC | 1,877 ms
54,112 KB |
testcase_47 | AC | 1,881 ms
52,504 KB |
testcase_48 | AC | 1,883 ms
55,892 KB |
testcase_49 | AC | 1,879 ms
54,568 KB |
testcase_50 | AC | 1,880 ms
54,408 KB |
testcase_51 | AC | 1,891 ms
54,448 KB |
testcase_52 | AC | 1,906 ms
56,696 KB |
testcase_53 | AC | 1,908 ms
52,596 KB |
testcase_54 | AC | 1,881 ms
52,780 KB |
testcase_55 | AC | 1,879 ms
56,024 KB |
testcase_56 | AC | 1,878 ms
52,540 KB |
testcase_57 | AC | 1,882 ms
54,568 KB |
testcase_58 | AC | 1,865 ms
56,052 KB |
testcase_59 | AC | 1,876 ms
54,616 KB |
testcase_60 | AC | 1,868 ms
56,096 KB |
testcase_61 | AC | 1,875 ms
55,884 KB |
testcase_62 | AC | 1,883 ms
56,504 KB |
testcase_63 | AC | 1,873 ms
55,200 KB |
testcase_64 | AC | 1,890 ms
56,732 KB |
testcase_65 | AC | 1,886 ms
52,704 KB |
testcase_66 | AC | 1,889 ms
54,644 KB |
testcase_67 | AC | 1,886 ms
57,828 KB |
testcase_68 | AC | 1,868 ms
53,244 KB |
testcase_69 | AC | 1,882 ms
52,524 KB |
testcase_70 | AC | 1,877 ms
54,636 KB |
testcase_71 | AC | 1,871 ms
52,440 KB |
testcase_72 | AC | 1,874 ms
54,660 KB |
testcase_73 | AC | 1,878 ms
52,360 KB |
testcase_74 | AC | 1,886 ms
54,796 KB |
testcase_75 | AC | 1,882 ms
51,480 KB |
testcase_76 | AC | 1,885 ms
52,704 KB |
testcase_77 | AC | 1,883 ms
53,656 KB |
testcase_78 | AC | 1,879 ms
54,584 KB |
testcase_79 | AC | 1,882 ms
54,556 KB |
testcase_80 | AC | 1,878 ms
54,512 KB |
testcase_81 | AC | 1,882 ms
52,508 KB |
testcase_82 | AC | 1,880 ms
54,484 KB |
testcase_83 | AC | 1,872 ms
52,500 KB |
testcase_84 | AC | 1,888 ms
54,432 KB |
testcase_85 | AC | 1,884 ms
54,444 KB |
testcase_86 | AC | 1,895 ms
54,104 KB |
testcase_87 | AC | 1,878 ms
52,664 KB |
testcase_88 | AC | 1,894 ms
54,536 KB |
testcase_89 | AC | 1,881 ms
55,912 KB |
testcase_90 | AC | 1,892 ms
55,928 KB |
testcase_91 | AC | 1,889 ms
55,632 KB |
testcase_92 | AC | 1,866 ms
55,896 KB |
testcase_93 | AC | 1,871 ms
54,480 KB |
testcase_94 | AC | 1,895 ms
56,608 KB |
testcase_95 | AC | 1,864 ms
54,956 KB |
testcase_96 | AC | 1,874 ms
54,544 KB |
testcase_97 | AC | 1,901 ms
54,592 KB |
testcase_98 | AC | 1,872 ms
56,372 KB |
testcase_99 | AC | 1,880 ms
55,272 KB |
ソースコード
import java.util.Arrays; import java.util.Scanner; public class Main { private int[][] dice; private int score = (int) -1e18; private int[][] table; private int bestScore = (int) -1e18; private int[][] bestTable; private SAState sa = new SAState(); private int[][] counts_row = new int[6][6]; private int[][] counts_column = new int[6][6]; private int[] part_score_row = new int[6]; private int[] part_score_column = new int[6]; public static void main(String[] args) throws Exception { new Main().run(); } private void run() { read(); solve(); write(); } private void read() { try (final Scanner in = new Scanner(System.in)) { dice = new int[36][6]; for (int i = 0; i < 36; i++) { for (int j = 0; j < 6; j++) { dice[i][j] = in.nextInt() - 1; if (i == 0 && j == 0) { Constants.watch.init(); } } } } catch (Exception e) { e.printStackTrace(); } } private void solve() { greedy(); // GLS(); kick(); // Utils.debug(bestScore); } private void greedy() { table = new int[6][6]; bestTable = new int[6][6]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { table[i][j] = i * 6 + j; } } score = calculateScore(); saveBest(); } private void saveBest() { if (score > bestScore) { bestScore = score; for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { bestTable[r][c] = table[r][c]; } } } } private void restoreBest() { score = bestScore; for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { table[r][c] = bestTable[r][c]; } } } private void kick() { sa.startTime = Constants.watch.getSecond(); sa.endTime = 1 * 1.75; int iterations = 0; final int mask = (1 << 5) - 1; sa.init(); for (;;) { boolean update = false; if ((++iterations & mask) == mask) { sa.update(); if (sa.isTLE()) { break; } } for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { for (int r22 = 0; r22 < 6; r22++) { for (int c22 = 0; c22 < 6; c22++) { int r1 = r; int c1 = c; int r2 = r22; int c2 = c22; if (r2 == r1 && c2 == c1) { continue; } int current1 = table[r1][c1]; int current2 = table[r2][c2]; int before = 0; before += part_score_row[r1]; if (r2 != r1) { before += part_score_row[r2]; } before += part_score_column[c1]; if (c2 != c1) { before += part_score_column[c2]; } table[r1][c1] = current2; table[r2][c2] = current1; int after_r1 = partScoreRow(counts_row, r1); int after_r2 = partScoreRow(counts_row, r2); int after_c1 = partScoreColumn(counts_column, c1); int after_c2 = partScoreColumn(counts_column, c2); int after = 0; after += after_r1; if (r2 != r1) { after += after_r2; } after += after_c1; if (c2 != c1) { after += after_c2; } int deltaScore = after - before; if (deltaScore > 0) { score += deltaScore; part_score_row[r1] = after_r1; part_score_row[r2] = after_r2; part_score_column[c1] = after_c1; part_score_column[c2] = after_c2; saveBest(); update = true; } else { table[r1][c1] = current1; table[r2][c2] = current2; } } } } } saveBest(); if (update) { continue; } restoreBest(); for (int i = 0; i < 4; i++) { int r1 = Constants.RNG.nextInt(6); int c1 = Constants.RNG.nextInt(6); int r2 = Constants.RNG.nextInt(6); int c2 = Constants.RNG.nextInt(6); while (r2 == r1 && c2 == c1) { r2 = Constants.RNG.nextInt(6); c2 = Constants.RNG.nextInt(6); } int current1 = table[r1][c1]; int current2 = table[r2][c2]; table[r1][c1] = current2; table[r2][c2] = current1; } score = calculateScore(); saveBest(); } } private void GLS() { sa.startTime = Constants.watch.getSecond(); sa.endTime = 1 * 1.75; final int[][][] penalties = new int[6][6][36]; for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { Arrays.fill(penalties[r][c], 1); } } int iterations = 0; final int mask = (1 << 5) - 1; sa.init(); for (;;) { if ((++iterations & mask) == mask) { sa.update(); if (sa.isTLE()) { break; } } double best = -1e9; int r0 = -1; int c0 = -1; for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { if (r == 0 && c == 0) { continue; } double util = 1.0 / (1.0 + part_score_row[r] + part_score_column[c]) / penalties[r][c][table[r][c]]; if (util > best) { best = util; r0 = r; c0 = c; } } } penalties[r0][c0][table[r0][c0]]++; for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { if (r == 0 && c == 0) { continue; } int r1 = r0; int c1 = c0; int r2 = r; int c2 = c; if (r2 == r1 && c2 == c1) { continue; } int current1 = table[r1][c1]; int current2 = table[r2][c2]; int before = 0; before += part_score_row[r1]; if (r2 != r1) { before += part_score_row[r2]; } before += part_score_column[c1]; if (c2 != c1) { before += part_score_column[c2]; } table[r1][c1] = current2; table[r2][c2] = current1; int after_r1 = partScoreRow(counts_row, r1); int after_r2 = partScoreRow(counts_row, r2); int after_c1 = partScoreColumn(counts_column, c1); int after_c2 = partScoreColumn(counts_column, c2); int after = 0; after += after_r1; if (r2 != r1) { after += after_r2; } after += after_c1; if (c2 != c1) { after += after_c2; } int deltaScore = after - before; table[r1][c1] = current1; table[r2][c2] = current2; int deltaPenalty = (penalties[r1][c1][table[r2][c2]] + penalties[r2][c2][table[r1][c1]]) - (penalties[r1][c1][table[r1][c1]] + penalties[r2][c2][table[r2][c2]]); if (deltaScore - 0.1 * deltaPenalty > 0) { table[r1][c1] = current2; table[r2][c2] = current1; score += deltaScore; part_score_row[r1] = after_r1; part_score_row[r2] = after_r2; part_score_column[c1] = after_c1; part_score_column[c2] = after_c2; saveBest(); } else { } } } } } private void multiSA() { int numRestart = 10; double startTime = Constants.watch.getSecond(); double endTime = 1 * 1.75; double remainTime = endTime - startTime; double startStartTemperature = 1; double endStartTemperature = 1e-9; for (double restart = 0; restart < numRestart; restart++) { sa.startTime = startTime + remainTime * restart / numRestart; sa.endTime = startTime + remainTime * (restart + 1) / numRestart; sa.startTemperature = endStartTemperature + (startStartTemperature - endStartTemperature) * ((numRestart - restart) / numRestart); sa.endTemperature = 1e-9; SA(); restoreBest(); } } private void SA() { sa.init(); for (;; ++sa.numIterations) { if ((sa.numIterations & ((1 << 10) - 1)) == 0) { sa.update(); if (sa.isTLE()) { break; } } mutate(); } } private void mutate() { change(); } private void change() { int v1 = sa.numIterations % 36; int r1 = v1 / 6; int c1 = v1 % 6; int r2 = Constants.RNG.nextInt(6); int c2 = Constants.RNG.nextInt(6); while (r2 == r1 && c2 == c1) { r2 = Constants.RNG.nextInt(6); c2 = Constants.RNG.nextInt(6); } int current1 = table[r1][c1]; int current2 = table[r2][c2]; int before = 0; before += part_score_row[r1]; if (r2 != r1) { before += part_score_row[r2]; } before += part_score_column[c1]; if (c2 != c1) { before += part_score_column[c2]; } table[r1][c1] = current2; table[r2][c2] = current1; int after_r1 = partScoreRow(counts_row, r1); int after_r2 = partScoreRow(counts_row, r2); int after_c1 = partScoreColumn(counts_column, c1); int after_c2 = partScoreColumn(counts_column, c2); int after = 0; after += after_r1; if (r2 != r1) { after += after_r2; } after += after_c1; if (c2 != c1) { after += after_c2; } int deltaScore = after - before; if (sa.accept(deltaScore)) { score += deltaScore; part_score_row[r1] = after_r1; part_score_row[r2] = after_r2; part_score_column[c1] = after_c1; part_score_column[c2] = after_c2; saveBest(); } else { table[r1][c1] = current1; table[r2][c2] = current2; } } private int calculateScore() { int score = 0; for (int r = 0; r < 6; r++) { final int partScoreRow = partScoreRow(counts_row, r); part_score_row[r] = partScoreRow; score += partScoreRow; } for (int c = 0; c < 6; c++) { final int partScoreColumn = partScoreColumn(counts_column, c); part_score_column[c] = partScoreColumn; score += partScoreColumn; } return score; } private int partScoreColumn(int[][] counts_column, int c) { Arrays.fill(counts_column[c], 0); int mask = (1 << 6) - 1; for (int r = 0; r < 6; r++) { int index = table[r][c]; int mask2 = 0; for (int i = 0; i < 6; i++) { counts_column[c][dice[index][i]]++; mask2 |= (1 << dice[index][i]); } mask &= mask2; } int part_score = 0; for (int i = 0; i < 6; i++) { if ((mask & (1 << i)) == 0) { continue; } part_score += 3; part_score += counts_column[c][i] - 6; } return part_score; } private int partScoreRow(int[][] counts_row, int r) { Arrays.fill(counts_row[r], 0); int mask = (1 << 6) - 1; for (int c = 0; c < 6; c++) { int index = table[r][c]; int mask2 = 0; for (int i = 0; i < 6; i++) { counts_row[r][dice[index][i]]++; mask2 |= (1 << dice[index][i]); } mask &= mask2; } int part_score = 0; for (int i = 0; i < 6; i++) { if ((mask & (1 << i)) == 0) { continue; } part_score += 3; part_score += counts_row[r][i] - 6; } return part_score; } private void write() { String[] s = new String[36]; for (int i = 0; i < s.length; i++) { s[i] = ""; } for (int r = 0; r < 6; r++) { for (int c = 0; c < 6; c++) { s[bestTable[r][c]] = (r + 1) + " " + (c + 1); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < 36; i++) { sb.append(s[i]).append('\n'); } System.out.print(sb.toString()); System.out.flush(); } } class SAState { public static final boolean useTime = true; public double startTime; public double endTime; public double time; public double startTemperature; public double endTemperature; public double inverseTemperature; public double lastAcceptTemperature; public double startRange; public double endRange; public double range; public int numIterations; public int validIterations; public int acceptIterations; private double[] log = new double[32768]; public SAState() { for (int i = 0; i < log.length; i++) { log[i] = Math.log((i + 0.5) / log.length); } } public void init() { numIterations = 0; validIterations = 0; acceptIterations = 0; startTime = useTime ? Constants.watch.getSecond() : numIterations; update(); lastAcceptTemperature = inverseTemperature; } public void update() { updateTime(); updateTemperature(); } public boolean useExp = !true; public void updateTemperature() { if (useExp) { double time0to1 = elapsedPercentage(startTime, endTime, time); double startY = startTemperature; double endY = endTemperature; double startX = Math.log(startY); double endX = Math.log(endY); double xStartToEnd = interpolate(startX, endX, time0to1); double temperature = Math.exp(xStartToEnd); inverseTemperature = 1.0 / temperature; } else { double time0to1 = elapsedPercentage(startTime, endTime, time); double startY = startTemperature; double endY = endTemperature; double temperature = interpolate(startY, endY, time0to1); inverseTemperature = 1.0 / temperature; } } private double elapsedPercentage(double min, double max, double v) { return (v - min) / (max - min); } private double interpolate(double v0, double v1, double d0to1) { return v0 + (v1 - v0) * d0to1; } public void updateRange() { range = endRange + (startRange - endRange) * Math.pow((endTime - time) / (endTime - startTime), 1.0); } public void updateTime() { time = useTime ? Constants.watch.getSecond() : numIterations; } public boolean isTLE() { return time >= endTime; } public boolean accept(double deltaScore) { return acceptB(deltaScore); } public boolean acceptB(double deltaScore) { validIterations++; if (deltaScore > -1e-9) { acceptIterations++; return true; } double d = deltaScore * inverseTemperature; if (d < -10) { return false; } if (log[Constants.RNG.nextInt() & 32767] < d) { acceptIterations++; lastAcceptTemperature = inverseTemperature; return true; } return false; } public boolean acceptS(double deltaScore) { validIterations++; if (deltaScore < 1e-9) { acceptIterations++; return true; } double d = -deltaScore * inverseTemperature; if (d < -10) { return false; } if (log[Constants.RNG.nextInt() & 32767] < d) { acceptIterations++; lastAcceptTemperature = inverseTemperature; return true; } return false; } } final class Utils { private Utils() { } public static final void debug(Object... o) { System.err.println(toString(o)); System.err.flush(); } public static final String toString(Object... o) { return Arrays.deepToString(o); } public static boolean isValid(int v, int min, int minUpper) { return v >= min && v < minUpper; } } class Watch { private long start; public Watch() { init(); } public double getSecond() { return (System.nanoTime() - start) * 1e-9; } public void init() { init(System.nanoTime()); } private void init(long start) { this.start = start; } public String getSecondString() { return toString(getSecond()); } public static final String toString(double second) { if (second < 60) { return String.format("%5.2fs", second); } else if (second < 60 * 60) { int minute = (int) (second / 60); return String.format("%2dm%2ds", minute, (int) (second % 60)); } else { int hour = (int) (second / (60 * 60)); int minute = (int) (second / 60); return String.format("%2dh%2dm%2ds", hour, minute % (60), (int) (second % 60)); } } } interface Constants { Watch watch = new Watch(); PCG_XSH_RR RNG = new PCG_XSH_RR(System.nanoTime()); } final class PCG_XSH_RR { private long state = 5342; public PCG_XSH_RR(final long state) { this.state = state; } public int nextInt() { final long oldstate = state; state = oldstate * 6364136223846793005L + 521L; final int xorshift = (int) (((oldstate >>> 18) ^ oldstate) >>> 27); final int rotation = (int) (oldstate >>> 59); return (xorshift >>> rotation) | (xorshift << (-rotation & 31)); } public int nextInt(int n) { return (int) (n * nextDouble()); } public double nextDouble() { return (nextInt() >>> 1) * 4.6566128730773926E-10; } }