結果

問題 No.5013 セクスタプル (open)
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2023-01-07 22:19:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,962 ms / 2,000 ms
コード長 14,147 bytes
コンパイル時間 2,261 ms
実行使用メモリ 56,076 KB
スコア 18,552
最終ジャッジ日時 2023-01-07 22:22:35
合計ジャッジ時間 204,170 ms
ジャッジサーバーID
(参考情報)
judge11 / judge16
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,942 ms
55,852 KB
testcase_01 AC 1,933 ms
55,944 KB
testcase_02 AC 1,937 ms
55,856 KB
testcase_03 AC 1,937 ms
55,884 KB
testcase_04 AC 1,962 ms
53,776 KB
testcase_05 AC 1,935 ms
55,796 KB
testcase_06 AC 1,938 ms
55,924 KB
testcase_07 AC 1,943 ms
56,016 KB
testcase_08 AC 1,936 ms
55,856 KB
testcase_09 AC 1,942 ms
53,792 KB
testcase_10 AC 1,935 ms
55,844 KB
testcase_11 AC 1,940 ms
55,736 KB
testcase_12 AC 1,937 ms
55,812 KB
testcase_13 AC 1,935 ms
55,872 KB
testcase_14 AC 1,940 ms
55,912 KB
testcase_15 AC 1,950 ms
53,880 KB
testcase_16 AC 1,940 ms
55,920 KB
testcase_17 AC 1,942 ms
55,776 KB
testcase_18 AC 1,945 ms
53,956 KB
testcase_19 AC 1,941 ms
55,768 KB
testcase_20 AC 1,938 ms
55,916 KB
testcase_21 AC 1,937 ms
55,652 KB
testcase_22 AC 1,935 ms
55,744 KB
testcase_23 AC 1,940 ms
53,824 KB
testcase_24 AC 1,938 ms
55,760 KB
testcase_25 AC 1,933 ms
55,884 KB
testcase_26 AC 1,943 ms
53,896 KB
testcase_27 AC 1,936 ms
55,676 KB
testcase_28 AC 1,946 ms
55,944 KB
testcase_29 AC 1,948 ms
55,876 KB
testcase_30 AC 1,941 ms
55,832 KB
testcase_31 AC 1,946 ms
56,012 KB
testcase_32 AC 1,943 ms
55,904 KB
testcase_33 AC 1,942 ms
55,792 KB
testcase_34 AC 1,940 ms
55,692 KB
testcase_35 AC 1,935 ms
55,796 KB
testcase_36 AC 1,938 ms
55,688 KB
testcase_37 AC 1,936 ms
55,748 KB
testcase_38 AC 1,937 ms
55,908 KB
testcase_39 AC 1,938 ms
55,816 KB
testcase_40 AC 1,936 ms
55,876 KB
testcase_41 AC 1,938 ms
55,720 KB
testcase_42 AC 1,936 ms
55,808 KB
testcase_43 AC 1,944 ms
53,980 KB
testcase_44 AC 1,940 ms
55,788 KB
testcase_45 AC 1,943 ms
53,820 KB
testcase_46 AC 1,936 ms
55,728 KB
testcase_47 AC 1,935 ms
55,860 KB
testcase_48 AC 1,936 ms
55,728 KB
testcase_49 AC 1,937 ms
55,888 KB
testcase_50 AC 1,938 ms
55,968 KB
testcase_51 AC 1,936 ms
56,044 KB
testcase_52 AC 1,939 ms
55,780 KB
testcase_53 AC 1,936 ms
55,868 KB
testcase_54 AC 1,937 ms
55,880 KB
testcase_55 AC 1,937 ms
55,848 KB
testcase_56 AC 1,936 ms
55,796 KB
testcase_57 AC 1,940 ms
55,916 KB
testcase_58 AC 1,936 ms
55,748 KB
testcase_59 AC 1,943 ms
54,000 KB
testcase_60 AC 1,934 ms
56,020 KB
testcase_61 AC 1,937 ms
55,860 KB
testcase_62 AC 1,941 ms
53,852 KB
testcase_63 AC 1,937 ms
55,768 KB
testcase_64 AC 1,938 ms
55,932 KB
testcase_65 AC 1,935 ms
55,892 KB
testcase_66 AC 1,937 ms
55,944 KB
testcase_67 AC 1,936 ms
55,744 KB
testcase_68 AC 1,941 ms
55,956 KB
testcase_69 AC 1,935 ms
55,896 KB
testcase_70 AC 1,944 ms
55,752 KB
testcase_71 AC 1,940 ms
55,740 KB
testcase_72 AC 1,939 ms
55,704 KB
testcase_73 AC 1,942 ms
53,876 KB
testcase_74 AC 1,935 ms
55,880 KB
testcase_75 AC 1,938 ms
55,848 KB
testcase_76 AC 1,936 ms
53,820 KB
testcase_77 AC 1,937 ms
55,976 KB
testcase_78 AC 1,938 ms
55,736 KB
testcase_79 AC 1,938 ms
55,776 KB
testcase_80 AC 1,935 ms
55,864 KB
testcase_81 AC 1,937 ms
55,952 KB
testcase_82 AC 1,937 ms
56,076 KB
testcase_83 AC 1,939 ms
55,924 KB
testcase_84 AC 1,933 ms
55,808 KB
testcase_85 AC 1,937 ms
55,916 KB
testcase_86 AC 1,940 ms
55,972 KB
testcase_87 AC 1,937 ms
55,828 KB
testcase_88 AC 1,936 ms
55,740 KB
testcase_89 AC 1,935 ms
55,940 KB
testcase_90 AC 1,940 ms
55,792 KB
testcase_91 AC 1,939 ms
55,884 KB
testcase_92 AC 1,937 ms
55,840 KB
testcase_93 AC 1,937 ms
55,924 KB
testcase_94 AC 1,941 ms
55,784 KB
testcase_95 AC 1,936 ms
55,872 KB
testcase_96 AC 1,939 ms
55,740 KB
testcase_97 AC 1,937 ms
55,872 KB
testcase_98 AC 1,937 ms
55,828 KB
testcase_99 AC 1,938 ms
53,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private long[] coef = new long[] { 1L, 1L << 6, 1L << 12, 1L << 18, 1L << 24, 1L << 30, };
    private int[] dice_mask = new int[36];
    private long[] dice_count = new long[36];
    private int score = (int) -1e9;
    private int[] table = new int[36];
    private int bestScore = (int) -1e9;
    private int[] bestTable = new int[36];
    private int[] part_score_row = new int[6];
    private long[] counts_row = new long[6];
    private int[] part_score_column = new int[6];
    private long[] counts_column = new long[6];
    private SAState sa = new SAState();

    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)) {
            for (int i = 0; i < 36; i++) {
                for (int j = 0; j < 6; j++) {
                    int dice = in.nextInt() - 1;
                    dice_mask[i] |= (1 << dice);
                    dice_count[i] += coef[dice];
                    if (i == 0 && j == 0) {
                        Constants.watch.init();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void solve() {
        greedy();
        multiSA();
        Utils.debug(bestScore, sa.numIterations);
    }

    private void greedy() {
        for (int r = 0; r < 6; r++) {
            for (int c = 0; c < 6; c++) {
                final int i = r * 6 + c;
                table[i] = i;
                counts_row[r] += dice_count[table[i]];
                counts_column[c] += dice_count[table[i]];
            }
        }
        score = calculateScore();
        saveBest();
    }

    private void saveBest() {
        if (score > bestScore) {
            bestScore = score;
            System.arraycopy(table, 0, bestTable, 0, table.length);
        }
    }

    private void restoreBest() {
        for (int i = 0; i < 6; i++) {
            counts_row[i] = 0;
            counts_column[i] = 0;
        }
        System.arraycopy(bestTable, 0, table, 0, table.length);
        for (int r = 0; r < 6; r++) {
            for (int c = 0; c < 6; c++) {
                final int i = r * 6 + c;
                counts_row[r] += dice_count[table[i]];
                counts_column[c] += dice_count[table[i]];
            }
        }
        score = calculateScore();
    }

    private void multiSA() {
        int numRestart = 100;
        double startTime = Constants.watch.getSecond();
        double endTime = 1.8;
        double remainTime = endTime - startTime;
        double startStartTemperature = 1.0 / 2;
        double endStartTemperature = 1.0 / 0.5;
        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 = 1.0 / 0.5;
            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;
        final int r1 = v1 / 6;
        final int c1 = v1 % 6;
        int v2 = Constants.RNG.nextInt(35);
        if (v2 >= v1) {
            v2++;
        }
        final int r2 = v2 / 6;
        final int c2 = v2 % 6;
        final int i1 = r1 * 6 + c1;
        final int current1 = table[i1];
        final int i2 = r2 * 6 + c2;
        final int current2 = table[i2];
        table[i1] = current2;
        table[i2] = current1;
        int before = 0;
        int after = 0;
        int after_r1 = 0;
        int after_r2 = 0;
        int after_c1 = 0;
        int after_c2 = 0;
        if (r2 != r1) {
            before += part_score_row[r1];
            before += part_score_row[r2];
            counts_row[r1] -= dice_count[current1];
            counts_row[r2] -= dice_count[current2];
            counts_row[r1] += dice_count[current2];
            counts_row[r2] += dice_count[current1];
            after_r1 = partScoreRow(r1);
            after_r2 = partScoreRow(r2);
            after += after_r1;
            after += after_r2;
        }
        if (c2 != c1) {
            before += part_score_column[c1];
            before += part_score_column[c2];
            counts_column[c1] -= dice_count[current1];
            counts_column[c2] -= dice_count[current2];
            counts_column[c1] += dice_count[current2];
            counts_column[c2] += dice_count[current1];
            after_c1 = partScoreColumn(c1);
            after_c2 = partScoreColumn(c2);
            after += after_c1;
            after += after_c2;
        }
        int deltaScore = after - before;
        if (sa.accept(deltaScore)) {
            score += deltaScore;
            if (r2 != r1) {
                part_score_row[r1] = after_r1;
                part_score_row[r2] = after_r2;
            }
            if (c2 != c1) {
                part_score_column[c1] = after_c1;
                part_score_column[c2] = after_c2;
            }
            saveBest();
        } else {
            table[i1] = current1;
            table[i2] = current2;
            if (r2 != r1) {
                counts_row[r1] -= dice_count[current2];
                counts_row[r2] -= dice_count[current1];
                counts_row[r1] += dice_count[current1];
                counts_row[r2] += dice_count[current2];
            }
            if (c2 != c1) {
                counts_column[c1] -= dice_count[current2];
                counts_column[c2] -= dice_count[current1];
                counts_column[c1] += dice_count[current1];
                counts_column[c2] += dice_count[current2];
            }
        }
    }

    private int calculateScore() {
        int score = 0;
        for (int r = 0; r < 6; r++) {
            final int partScoreRow = partScoreRow(r);
            part_score_row[r] = partScoreRow;
            score += partScoreRow;
        }
        for (int c = 0; c < 6; c++) {
            final int partScoreColumn = partScoreColumn(c);
            part_score_column[c] = partScoreColumn;
            score += partScoreColumn;
        }
        return score;
    }

    private int partScoreRow(int r) {
        int mask = dice_mask[table[r * 6 + 0]] & dice_mask[table[r * 6 + 1]] & dice_mask[table[r * 6 + 2]] & dice_mask[table[r * 6 + 3]] & dice_mask[table[r * 6 + 4]] & dice_mask[table[r * 6 + 5]];
        final long v = counts_row[r];
        int part_score = (int) (((mask & (1 << 0)) == 0 ? 0 : -3 + (v & 63)) + ((mask & (1 << 1)) == 0 ? 0 : -3 + ((v >>> 6) & 63)) + ((mask & (1 << 2)) == 0 ? 0 : -3 + ((v >>> 12) & 63)) + ((mask & (1 << 3)) == 0 ? 0 : -3 + ((v >>> 18) & 63)) + ((mask & (1 << 4)) == 0 ? 0 : -3 + ((v >>> 24) & 63)) + ((mask & (1 << 5)) == 0 ? 0 : -3 + ((v >>> 30) & 63)));
        return part_score;
    }

    private int partScoreColumn(int c) {
        int mask = dice_mask[table[0 * 6 + c]] & dice_mask[table[1 * 6 + c]] & dice_mask[table[2 * 6 + c]] & dice_mask[table[3 * 6 + c]] & dice_mask[table[4 * 6 + c]] & dice_mask[table[5 * 6 + c]];
        final long v = counts_column[c];
        int part_score = (int) (((mask & (1 << 0)) == 0 ? 0 : -3 + (v & 63)) + ((mask & (1 << 1)) == 0 ? 0 : -3 + ((v >>> 6) & 63)) + ((mask & (1 << 2)) == 0 ? 0 : -3 + ((v >>> 12) & 63)) + ((mask & (1 << 3)) == 0 ? 0 : -3 + ((v >>> 18) & 63)) + ((mask & (1 << 4)) == 0 ? 0 : -3 + ((v >>> 24) & 63)) + ((mask & (1 << 5)) == 0 ? 0 : -3 + ((v >>> 30) & 63)));
        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 * 6 + 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 {
        inverseTemperature = interpolate(startTemperature, endTemperature, elapsedPercentage(startTime, endTime, time));
//        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;
    }
}
0