結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-12-17 16:35:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,745 ms / 5,000 ms
コード長 12,562 bytes
コンパイル時間 4,097 ms
実行使用メモリ 75,660 KB
スコア 18,474,568,742
最終ジャッジ日時 2022-12-17 16:45:50
合計ジャッジ時間 620,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,717 ms
61,260 KB
testcase_01 AC 4,723 ms
60,760 KB
testcase_02 AC 4,718 ms
60,108 KB
testcase_03 AC 4,711 ms
60,344 KB
testcase_04 AC 4,712 ms
60,268 KB
testcase_05 AC 4,715 ms
63,332 KB
testcase_06 AC 4,729 ms
60,584 KB
testcase_07 AC 4,713 ms
64,876 KB
testcase_08 AC 4,722 ms
60,372 KB
testcase_09 AC 4,714 ms
61,084 KB
testcase_10 AC 4,720 ms
60,572 KB
testcase_11 AC 4,714 ms
60,180 KB
testcase_12 AC 4,707 ms
60,036 KB
testcase_13 AC 4,736 ms
59,652 KB
testcase_14 AC 4,704 ms
60,116 KB
testcase_15 AC 4,713 ms
60,264 KB
testcase_16 AC 4,717 ms
61,428 KB
testcase_17 AC 4,724 ms
60,624 KB
testcase_18 AC 4,704 ms
60,148 KB
testcase_19 AC 4,714 ms
60,940 KB
testcase_20 AC 4,720 ms
61,108 KB
testcase_21 AC 4,711 ms
62,716 KB
testcase_22 AC 4,710 ms
62,688 KB
testcase_23 AC 4,725 ms
62,540 KB
testcase_24 AC 4,718 ms
62,468 KB
testcase_25 AC 4,710 ms
64,772 KB
testcase_26 AC 4,720 ms
64,416 KB
testcase_27 AC 4,720 ms
65,132 KB
testcase_28 AC 4,712 ms
64,576 KB
testcase_29 AC 4,718 ms
65,396 KB
testcase_30 AC 4,715 ms
65,652 KB
testcase_31 AC 4,716 ms
64,996 KB
testcase_32 AC 4,713 ms
65,592 KB
testcase_33 AC 4,712 ms
65,916 KB
testcase_34 AC 4,716 ms
67,820 KB
testcase_35 AC 4,716 ms
65,312 KB
testcase_36 AC 4,719 ms
66,532 KB
testcase_37 AC 4,721 ms
65,596 KB
testcase_38 AC 4,714 ms
65,888 KB
testcase_39 AC 4,722 ms
65,320 KB
testcase_40 AC 4,722 ms
65,984 KB
testcase_41 AC 4,722 ms
65,580 KB
testcase_42 AC 4,711 ms
69,996 KB
testcase_43 AC 4,712 ms
65,236 KB
testcase_44 AC 4,744 ms
67,984 KB
testcase_45 AC 4,716 ms
66,696 KB
testcase_46 AC 4,740 ms
65,412 KB
testcase_47 AC 4,716 ms
65,492 KB
testcase_48 AC 4,745 ms
67,824 KB
testcase_49 AC 4,701 ms
71,524 KB
testcase_50 AC 4,743 ms
70,044 KB
testcase_51 AC 4,720 ms
69,868 KB
testcase_52 AC 4,726 ms
72,392 KB
testcase_53 AC 4,727 ms
72,704 KB
testcase_54 AC 4,742 ms
69,704 KB
testcase_55 AC 4,715 ms
69,984 KB
testcase_56 AC 4,720 ms
70,016 KB
testcase_57 AC 4,719 ms
69,804 KB
testcase_58 AC 4,739 ms
72,368 KB
testcase_59 AC 4,720 ms
71,560 KB
testcase_60 AC 4,745 ms
72,240 KB
testcase_61 AC 4,723 ms
74,144 KB
testcase_62 AC 4,725 ms
73,456 KB
testcase_63 AC 4,719 ms
73,884 KB
testcase_64 AC 4,741 ms
73,888 KB
testcase_65 AC 4,720 ms
73,728 KB
testcase_66 AC 4,716 ms
72,200 KB
testcase_67 AC 4,712 ms
71,848 KB
testcase_68 AC 4,722 ms
73,596 KB
testcase_69 AC 4,722 ms
73,244 KB
testcase_70 AC 4,734 ms
74,080 KB
testcase_71 AC 4,720 ms
73,712 KB
testcase_72 AC 4,714 ms
73,800 KB
testcase_73 AC 4,725 ms
74,012 KB
testcase_74 AC 4,719 ms
74,100 KB
testcase_75 AC 4,740 ms
73,472 KB
testcase_76 AC 4,718 ms
73,600 KB
testcase_77 AC 4,724 ms
73,380 KB
testcase_78 AC 4,716 ms
73,664 KB
testcase_79 AC 4,727 ms
74,020 KB
testcase_80 AC 4,718 ms
73,728 KB
testcase_81 AC 4,730 ms
74,036 KB
testcase_82 AC 4,717 ms
73,924 KB
testcase_83 AC 4,737 ms
73,748 KB
testcase_84 AC 4,710 ms
74,140 KB
testcase_85 AC 4,724 ms
74,068 KB
testcase_86 AC 4,724 ms
74,668 KB
testcase_87 AC 4,719 ms
73,916 KB
testcase_88 AC 4,722 ms
73,696 KB
testcase_89 AC 4,726 ms
71,928 KB
testcase_90 AC 4,722 ms
73,600 KB
testcase_91 AC 4,713 ms
73,412 KB
testcase_92 AC 4,719 ms
71,600 KB
testcase_93 AC 4,726 ms
73,560 KB
testcase_94 AC 4,715 ms
75,660 KB
testcase_95 AC 4,712 ms
73,476 KB
testcase_96 AC 4,719 ms
73,744 KB
testcase_97 AC 4,716 ms
73,708 KB
testcase_98 AC 4,715 ms
73,512 KB
testcase_99 AC 4,724 ms
73,756 KB
testcase_100 AC 4,716 ms
73,072 KB
testcase_101 AC 4,722 ms
74,636 KB
testcase_102 AC 4,716 ms
74,016 KB
testcase_103 AC 4,718 ms
73,704 KB
testcase_104 AC 4,711 ms
73,524 KB
testcase_105 AC 4,718 ms
73,972 KB
testcase_106 AC 4,719 ms
74,476 KB
testcase_107 AC 4,718 ms
74,108 KB
testcase_108 AC 4,716 ms
73,628 KB
testcase_109 AC 4,720 ms
74,096 KB
testcase_110 AC 4,703 ms
73,616 KB
testcase_111 AC 4,713 ms
75,196 KB
testcase_112 AC 4,718 ms
73,644 KB
testcase_113 AC 4,722 ms
73,448 KB
testcase_114 AC 4,718 ms
73,216 KB
testcase_115 AC 4,718 ms
73,460 KB
testcase_116 AC 4,719 ms
73,844 KB
testcase_117 AC 4,718 ms
73,624 KB
testcase_118 AC 4,713 ms
74,152 KB
testcase_119 AC 4,714 ms
73,692 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 = 4.5;
        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