結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-12-17 16:21:07
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 12,559 bytes
コンパイル時間 3,537 ms
実行使用メモリ 77,116 KB
スコア 18,012,568,152
最終ジャッジ日時 2022-12-17 16:32:05
合計ジャッジ時間 650,060 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,984 ms
60,680 KB
testcase_01 AC 4,967 ms
60,524 KB
testcase_02 AC 4,979 ms
60,376 KB
testcase_03 AC 4,989 ms
60,136 KB
testcase_04 AC 4,971 ms
60,680 KB
testcase_05 AC 4,981 ms
60,200 KB
testcase_06 AC 4,980 ms
60,584 KB
testcase_07 AC 4,958 ms
60,676 KB
testcase_08 AC 4,986 ms
61,016 KB
testcase_09 AC 4,963 ms
60,816 KB
testcase_10 AC 4,995 ms
60,588 KB
testcase_11 AC 4,961 ms
60,808 KB
testcase_12 AC 4,961 ms
61,052 KB
testcase_13 AC 4,972 ms
60,360 KB
testcase_14 AC 4,966 ms
60,456 KB
testcase_15 AC 4,976 ms
60,624 KB
testcase_16 AC 4,963 ms
61,504 KB
testcase_17 AC 4,973 ms
60,704 KB
testcase_18 AC 4,975 ms
61,176 KB
testcase_19 AC 4,966 ms
60,880 KB
testcase_20 AC 4,973 ms
61,004 KB
testcase_21 AC 4,974 ms
60,884 KB
testcase_22 AC 4,972 ms
61,424 KB
testcase_23 AC 4,973 ms
60,724 KB
testcase_24 AC 4,986 ms
60,676 KB
testcase_25 AC 4,957 ms
60,944 KB
testcase_26 AC 4,980 ms
60,728 KB
testcase_27 AC 4,983 ms
60,800 KB
testcase_28 AC 4,973 ms
61,132 KB
testcase_29 AC 4,967 ms
60,812 KB
testcase_30 AC 4,970 ms
61,404 KB
testcase_31 AC 4,977 ms
60,788 KB
testcase_32 AC 4,967 ms
61,108 KB
testcase_33 AC 4,962 ms
60,644 KB
testcase_34 AC 4,982 ms
61,180 KB
testcase_35 AC 4,979 ms
60,368 KB
testcase_36 AC 4,969 ms
61,628 KB
testcase_37 AC 4,965 ms
61,892 KB
testcase_38 AC 4,985 ms
60,948 KB
testcase_39 AC 4,970 ms
60,212 KB
testcase_40 AC 4,972 ms
61,108 KB
testcase_41 AC 4,972 ms
63,016 KB
testcase_42 AC 4,975 ms
60,640 KB
testcase_43 AC 4,964 ms
60,900 KB
testcase_44 TLE -
testcase_45 AC 4,977 ms
60,344 KB
testcase_46 AC 4,974 ms
60,412 KB
testcase_47 AC 4,968 ms
61,836 KB
testcase_48 AC 4,967 ms
60,580 KB
testcase_49 AC 4,960 ms
60,704 KB
testcase_50 AC 4,965 ms
61,140 KB
testcase_51 AC 4,971 ms
60,156 KB
testcase_52 AC 4,972 ms
60,784 KB
testcase_53 AC 4,968 ms
60,312 KB
testcase_54 AC 4,979 ms
61,324 KB
testcase_55 AC 4,973 ms
61,484 KB
testcase_56 AC 4,963 ms
60,920 KB
testcase_57 AC 4,974 ms
61,864 KB
testcase_58 AC 4,974 ms
60,596 KB
testcase_59 AC 4,977 ms
63,476 KB
testcase_60 AC 4,964 ms
60,092 KB
testcase_61 AC 4,967 ms
61,028 KB
testcase_62 AC 4,966 ms
60,840 KB
testcase_63 AC 4,972 ms
61,160 KB
testcase_64 AC 4,960 ms
61,940 KB
testcase_65 AC 4,968 ms
60,892 KB
testcase_66 AC 4,970 ms
60,788 KB
testcase_67 TLE -
testcase_68 AC 4,970 ms
61,532 KB
testcase_69 AC 4,981 ms
60,688 KB
testcase_70 AC 4,967 ms
60,500 KB
testcase_71 AC 4,971 ms
61,296 KB
testcase_72 AC 4,963 ms
60,928 KB
testcase_73 AC 4,965 ms
61,580 KB
testcase_74 AC 4,963 ms
61,012 KB
testcase_75 AC 4,970 ms
60,604 KB
testcase_76 AC 4,991 ms
60,680 KB
testcase_77 AC 4,974 ms
63,992 KB
testcase_78 AC 4,967 ms
61,892 KB
testcase_79 AC 4,965 ms
61,100 KB
testcase_80 AC 4,971 ms
60,512 KB
testcase_81 AC 4,962 ms
61,196 KB
testcase_82 AC 4,961 ms
61,160 KB
testcase_83 AC 4,973 ms
61,232 KB
testcase_84 AC 4,971 ms
63,060 KB
testcase_85 AC 4,974 ms
60,560 KB
testcase_86 AC 4,980 ms
61,056 KB
testcase_87 AC 4,981 ms
61,204 KB
testcase_88 AC 4,976 ms
63,476 KB
testcase_89 AC 4,965 ms
61,520 KB
testcase_90 AC 4,968 ms
61,112 KB
testcase_91 AC 4,973 ms
61,440 KB
testcase_92 TLE -
testcase_93 AC 4,965 ms
60,756 KB
testcase_94 AC 4,969 ms
65,108 KB
testcase_95 AC 4,975 ms
62,624 KB
testcase_96 AC 4,974 ms
66,108 KB
testcase_97 AC 4,968 ms
61,864 KB
testcase_98 AC 4,968 ms
62,684 KB
testcase_99 AC 4,969 ms
62,780 KB
testcase_100 AC 4,966 ms
64,432 KB
testcase_101 AC 4,975 ms
62,380 KB
testcase_102 AC 4,965 ms
60,440 KB
testcase_103 AC 4,975 ms
62,652 KB
testcase_104 AC 4,963 ms
63,068 KB
testcase_105 AC 4,977 ms
60,948 KB
testcase_106 AC 4,976 ms
61,580 KB
testcase_107 AC 4,967 ms
60,656 KB
testcase_108 AC 4,987 ms
60,996 KB
testcase_109 AC 4,985 ms
75,148 KB
testcase_110 AC 4,981 ms
77,116 KB
testcase_111 AC 4,969 ms
63,172 KB
testcase_112 AC 4,978 ms
63,960 KB
testcase_113 AC 4,971 ms
61,116 KB
testcase_114 AC 4,977 ms
73,628 KB
testcase_115 AC 4,971 ms
63,908 KB
testcase_116 AC 4,997 ms
74,324 KB
testcase_117 AC 4,970 ms
62,728 KB
testcase_118 AC 4,972 ms
61,056 KB
testcase_119 AC 4,984 ms
63,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    final int e = 750;
    final int e2 = e / 5;
    private int N;
    private int Q;
    private int WT;
    private int ST;
    private int[] W;
    private int[] L;
    private int[] R;
    private int[] P;
    private long[] cumsumW;
    private long score;
    private long bestScore;
    private SAState sa = new SAState();

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

    private void run() {
        read();
        init();
        greedy();
        multiSA();
        write();
    }

    private void read() {
        try (Scanner in = new Scanner(System.in)) {
            N = in.nextInt();
            Constants.watch.init();
            Q = in.nextInt();
            WT = in.nextInt();
            ST = in.nextInt();
            W = IntStream.range(0, N).map(i -> in.nextInt()).toArray();
            L = new int[Q];
            R = new int[Q];
            IntStream.range(0, Q).forEach(i -> {
                L[i] = in.nextInt() - 1;
                R[i] = in.nextInt() - 1;
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        cumsumW = new long[N + 1];
        IntStream.range(0, N).forEach(i -> cumsumW[i + 1] = cumsumW[i] + W[i]);
    }

    private void greedy() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            list.add(i);
        }
        Collections.sort(list, (o1, o2) -> {
            {
                if (L[o1] / e < L[o2] / e) {
                    return -1;
                }
                if (L[o1] / e > L[o2] / e) {
                    return 1;
                }
                int coef = (L[o1] / e) % 2 == 0 ? 1 : -1;
                if (R[o1] / e < R[o2] / e) {
                    return coef * -1;
                }
                if (R[o1] / e > R[o2] / e) {
                    return coef * 1;
                }
            }
            {
                if (L[o1] / e2 < L[o2] / e2) {
                    return -1;
                }
                if (L[o1] / e2 > L[o2] / e2) {
                    return 1;
                }
                int coef = (L[o1] / e2) % 2 == 0 ? 1 : -1;
                if (R[o1] / e2 < R[o2] / e2) {
                    return coef * -1;
                }
                if (R[o1] / e2 > R[o2] / e2) {
                    return coef * 1;
                }
            }
            return 0;
        });
        P = new int[N];
        for (int i = 0; i < P.length; i++) {
            P[i] = list.get(i).intValue();
        }
        score = calculateScore(P, N);
        Utils.debug("greedy", "score", score);
    }

    private void multiSA() {
        int numRestart = 1;
        double startTime = Constants.watch.getSecond();
        double endTime = 5 - 0.25;
        double remainTime = endTime - startTime;
        double startStartTemperature = 1e4;
        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();
        }
        Utils.debug("multiSA", "score", score, "time", Constants.watch.getSecondString());
    }

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

    private void mutate() {
        reverse();
    }

    private void reverse() {
        int l = Constants.RNG.nextInt(N);
        final int d = 10;
        int r = l - d + Constants.RNG.nextInt(2 * d);
        while (r == l || r < 0 || r >= N) {
            r = l - d + Constants.RNG.nextInt(2 * d);
        }
        if (l > r) {
            int swap = l;
            l = r;
            r = swap;
        }
        long before = calculatePartScore(P, l) + (r + 1 >= N ? 0 : calculatePartScore(P, r + 1));
        {
            int swap = P[l];
            P[l] = P[r];
            P[r] = swap;
        }
        long after = calculatePartScore(P, l) + (r + 1 >= N ? 0 : calculatePartScore(P, r + 1));
        {
            int swap = P[l];
            P[l] = P[r];
            P[r] = swap;
        }
        long deltaScore = after - before;
        if (sa.accept(deltaScore)) {
            score += deltaScore;
            for (; l < r; l++, r--) {
                int swap = P[l];
                P[l] = P[r];
                P[r] = swap;
            }
        } else {
        }
    }

    private void write() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < P.length; i++) {
            if (i > 0) {
                sb.append(" ");
            }
            sb.append(1 + P[i]);
        }
        System.out.println(sb.toString());
        System.out.flush();
    }

    private long calculateScore(int[] P, int N) {
        long T = 0;
        for (int pi = 0; pi < P.length; pi++) {
            T += calculatePartScore(P, pi);
        }
        return T;
    }

    private long calculatePartScore(int[] P, int pi) {
        if (pi - 1 < 0) {
            return cumsumW[1 + R[P[0]]] - cumsumW[L[P[0]]];
        } else {
            int minL = Math.min(L[P[pi - 1]], L[P[pi]]);
            int maxL = Math.max(L[P[pi - 1]], L[P[pi]]);
            int minR = Math.min(R[P[pi - 1]], R[P[pi]]);
            int maxR = Math.max(R[P[pi - 1]], R[P[pi]]);
            return cumsumW[maxL] - cumsumW[minL] + cumsumW[1 + maxR] - cumsumW[1 + minR];
        }
    }
}

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

    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() {
        double time0to1 = elapsedPercentage(startTime, endTime, time);
        double startY = startRange;
        double endY = endRange;
        range = interpolate(startY, endY, time0to1);
    }

    public void updateTime() {
        time = useTime ? Constants.watch.getSecond() : numIterations;
    }

    public boolean isTLE() {
        return time >= endTime;
    }

    public boolean accept(double deltaScore) {
        return acceptS(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 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;
    }
}

interface Constants {
    Watch watch = new Watch();
    PCG_XSH_RR RNG = new PCG_XSH_RR(System.nanoTime());
}
0