結果

問題 No.5005 3-SAT
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-04-29 17:36:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,865 ms / 2,000 ms
コード長 10,268 bytes
コンパイル時間 4,415 ms
実行使用メモリ 47,320 KB
スコア 100,066
最終ジャッジ日時 2022-04-29 17:39:31
合計ジャッジ時間 195,573 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,856 ms
46,916 KB
testcase_01 AC 1,856 ms
47,064 KB
testcase_02 AC 1,857 ms
47,096 KB
testcase_03 AC 1,856 ms
47,168 KB
testcase_04 AC 1,854 ms
47,128 KB
testcase_05 AC 1,859 ms
47,060 KB
testcase_06 AC 1,858 ms
47,088 KB
testcase_07 AC 1,857 ms
47,064 KB
testcase_08 AC 1,856 ms
46,952 KB
testcase_09 AC 1,856 ms
46,988 KB
testcase_10 AC 1,856 ms
47,128 KB
testcase_11 AC 1,859 ms
46,920 KB
testcase_12 AC 1,858 ms
47,024 KB
testcase_13 AC 1,858 ms
46,968 KB
testcase_14 AC 1,858 ms
46,932 KB
testcase_15 AC 1,857 ms
46,940 KB
testcase_16 AC 1,854 ms
47,152 KB
testcase_17 AC 1,857 ms
47,036 KB
testcase_18 AC 1,857 ms
46,692 KB
testcase_19 AC 1,855 ms
47,184 KB
testcase_20 AC 1,856 ms
46,932 KB
testcase_21 AC 1,857 ms
46,932 KB
testcase_22 AC 1,855 ms
47,200 KB
testcase_23 AC 1,857 ms
46,932 KB
testcase_24 AC 1,858 ms
47,092 KB
testcase_25 AC 1,855 ms
47,144 KB
testcase_26 AC 1,856 ms
46,988 KB
testcase_27 AC 1,856 ms
46,936 KB
testcase_28 AC 1,854 ms
47,004 KB
testcase_29 AC 1,857 ms
47,112 KB
testcase_30 AC 1,857 ms
47,004 KB
testcase_31 AC 1,857 ms
47,048 KB
testcase_32 AC 1,853 ms
47,232 KB
testcase_33 AC 1,857 ms
47,040 KB
testcase_34 AC 1,856 ms
47,012 KB
testcase_35 AC 1,856 ms
47,176 KB
testcase_36 AC 1,857 ms
47,100 KB
testcase_37 AC 1,856 ms
47,196 KB
testcase_38 AC 1,854 ms
47,080 KB
testcase_39 AC 1,857 ms
47,040 KB
testcase_40 AC 1,856 ms
47,104 KB
testcase_41 AC 1,855 ms
47,128 KB
testcase_42 AC 1,856 ms
47,088 KB
testcase_43 AC 1,857 ms
47,116 KB
testcase_44 AC 1,857 ms
47,056 KB
testcase_45 AC 1,855 ms
46,992 KB
testcase_46 AC 1,857 ms
47,024 KB
testcase_47 AC 1,856 ms
46,980 KB
testcase_48 AC 1,857 ms
47,164 KB
testcase_49 AC 1,855 ms
47,160 KB
testcase_50 AC 1,856 ms
47,060 KB
testcase_51 AC 1,856 ms
47,048 KB
testcase_52 AC 1,857 ms
47,152 KB
testcase_53 AC 1,856 ms
47,172 KB
testcase_54 AC 1,858 ms
47,048 KB
testcase_55 AC 1,856 ms
47,152 KB
testcase_56 AC 1,858 ms
47,052 KB
testcase_57 AC 1,857 ms
47,112 KB
testcase_58 AC 1,855 ms
47,320 KB
testcase_59 AC 1,857 ms
46,952 KB
testcase_60 AC 1,858 ms
47,232 KB
testcase_61 AC 1,857 ms
46,892 KB
testcase_62 AC 1,858 ms
47,104 KB
testcase_63 AC 1,859 ms
47,120 KB
testcase_64 AC 1,858 ms
47,132 KB
testcase_65 AC 1,858 ms
46,960 KB
testcase_66 AC 1,858 ms
47,028 KB
testcase_67 AC 1,857 ms
47,040 KB
testcase_68 AC 1,857 ms
47,116 KB
testcase_69 AC 1,857 ms
47,024 KB
testcase_70 AC 1,859 ms
47,088 KB
testcase_71 AC 1,856 ms
47,148 KB
testcase_72 AC 1,858 ms
47,248 KB
testcase_73 AC 1,858 ms
47,064 KB
testcase_74 AC 1,859 ms
47,152 KB
testcase_75 AC 1,859 ms
47,144 KB
testcase_76 AC 1,860 ms
47,144 KB
testcase_77 AC 1,856 ms
47,056 KB
testcase_78 AC 1,859 ms
47,020 KB
testcase_79 AC 1,857 ms
47,104 KB
testcase_80 AC 1,857 ms
47,212 KB
testcase_81 AC 1,857 ms
47,168 KB
testcase_82 AC 1,861 ms
47,244 KB
testcase_83 AC 1,857 ms
46,900 KB
testcase_84 AC 1,859 ms
46,996 KB
testcase_85 AC 1,858 ms
47,196 KB
testcase_86 AC 1,861 ms
47,152 KB
testcase_87 AC 1,864 ms
46,960 KB
testcase_88 AC 1,859 ms
47,276 KB
testcase_89 AC 1,858 ms
47,104 KB
testcase_90 AC 1,859 ms
47,068 KB
testcase_91 AC 1,855 ms
47,092 KB
testcase_92 AC 1,859 ms
47,112 KB
testcase_93 AC 1,856 ms
47,036 KB
testcase_94 AC 1,856 ms
47,172 KB
testcase_95 AC 1,865 ms
47,196 KB
testcase_96 AC 1,858 ms
46,924 KB
testcase_97 AC 1,858 ms
47,228 KB
testcase_98 AC 1,859 ms
47,088 KB
testcase_99 AC 1,862 ms
47,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    private int N = 1 << 11;
    private int M = 1 << 8;
    private int[] a = new int[N];
    private int[] b = new int[N];
    private int[] c = new int[N];
    private int[] p = new int[N];
    private int[] q = new int[N];
    private int[] r = new int[N];

    private int score;
    private int bestScore;
    private int[] solution = new int[M];
    private int[] bestSolution = new int[M];

    int[][] penalties = new int[M][2];

    public static Watch watch = new Watch();
    public static XorShift rng = new XorShift(System.nanoTime());
    public static SAState sa = new SAState();

    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        read();
        solve();
        write();
    }

    private void read() {
        try (Scanner in = new Scanner(System.in)) {
            for (int i = 0; i < N; i++) {
                a[i] = in.nextInt();
                b[i] = in.nextInt();
                c[i] = in.nextInt();
                p[i] = in.nextInt();
                q[i] = in.nextInt();
                r[i] = in.nextInt();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void solve() {
        score = calculateScore(solution);
        multiSA();
    }

    private int calculateScore(int[] out2) {
        for (int i = 0; i < N; i++) {
            if (out2[a[i]] == p[i] || out2[b[i]] == q[i] || out2[c[i]] == r[i]) {
            } else {
                return i;
            }
        }

        return N;
    }

    private void multiSA() {
        int numRestart = 10;

        double startTime = watch.getSecond();
        double endTime = 1.8;
        double remainTime = endTime - startTime;

        double startStartTemperature = 10;
        double endStartTemperature = 10;
        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 = 0;
            for (int i = 0; i < M; i++) {
                Arrays.fill(penalties[i], 0);
            }
            GLS();
        }
        loadBest();
        Utils.debug("MultiSA", score, watch.getSecond());
    }

    private void GLS() {
        double second = 0.1 + watch.getSecond();
        sa.init();
        for (;; ++sa.numIterations) {
            if ((sa.numIterations & ((1 << 8) - 1)) == 0) {
                sa.update();
                if (sa.time > second) {
                    second += 0.1;
                    Utils.debug(sa.numIterations, String.format("%.2f%%", 100.0 * sa.validIterations / sa.numIterations), String.format("%.2f%%", 100.0 * sa.acceptIterations / sa.validIterations), String.format("%d", score), String.format("%d", bestScore), String.format("%.6f", 1.0 / sa.inverseTemperature), String.format("%.6f", 1.0 / sa.lastAcceptTemperature));
                }
                if (sa.isTLE()) {
                    break;
                }
            }

            mutate();
        }
    }

    private void mutate() {
        change();
    }

    private void change() {

        int minIndex = -1;
        int min = (int) 1e9;
        for (int i = 0; i < M; i++) {
            if (penalties[i][solution[i]] < min) {
                min = penalties[i][solution[i]];
                minIndex = i;
            }
        }
        penalties[minIndex][solution[minIndex]]++;

        int index = minIndex;

        int beforePenalty = penalties[index][solution[index]];

        solution[index] = 1 - solution[index];

        double deltaScore = calculateScore(solution) - score;

        int afterPenalty = penalties[index][solution[index]];
        double deltaPenalty = 1e0 * (afterPenalty - beforePenalty);
        sa.validIterations++;
        if (deltaScore > deltaPenalty) {
            sa.acceptIterations++;
            score += deltaScore;
            saveBest();
        } else {
            solution[index] = 1 - solution[index];
        }
    }

    private void write() {
        StringBuilder sb = new StringBuilder();
        IntStream.range(0, M).forEach(i -> sb.append(solution[i]));
        System.out.println(sb.reverse().toString());
        System.out.flush();
    }

    private void saveBest() {
        if (score > bestScore) {
            bestScore = score;
            System.arraycopy(solution, 0, bestSolution, 0, M);
        }
    }

    private void loadBest() {
        score = bestScore;
        System.arraycopy(bestSolution, 0, solution, 0, M);
    }

}

final class Utils {
    private Utils() {
    }

    public static final void debug(Object... o) {
        System.err.println(toString(o));
    }

    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));
        }
    }

}

class XorShift {
    private int w = 88675123;
    private int x = 123456789;
    private int y = 362436069;
    private int z = 521288629;

    public XorShift(long l) {
        x = (int) l;
    }

    public int nextInt() {
        final int t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        w = w ^ (w >>> 19) ^ (t ^ (t >>> 8));
        return w;
    }

    public long nextLong() {
        return ((long) nextInt() << 32) ^ (long) nextInt();
    }

    public double nextDouble() {
        return (nextInt() >>> 1) * 4.6566128730773926E-10;
    }

    public int nextInt(int n) {
        return (int) (n * nextDouble());
    }
}

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 ? Main.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 ? Main.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[Main.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[Main.rng.nextInt() & 32767] < d) {
            acceptIterations++;
            lastAcceptTemperature = inverseTemperature;
            return true;
        }
        return false;
    }

}
0