結果

問題 No.5011 Better Mo's Algorithm is Needed!! (Weighted)
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-12-17 16:22:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,998 ms / 5,000 ms
コード長 12,559 bytes
コンパイル時間 3,518 ms
実行使用メモリ 78,108 KB
スコア 40,533,875,699
最終ジャッジ日時 2022-12-17 16:33:45
合計ジャッジ時間 651,423 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,969 ms
60,568 KB
testcase_01 AC 4,967 ms
60,972 KB
testcase_02 AC 4,967 ms
60,708 KB
testcase_03 AC 4,998 ms
62,932 KB
testcase_04 AC 4,960 ms
60,284 KB
testcase_05 AC 4,974 ms
61,216 KB
testcase_06 AC 4,981 ms
61,920 KB
testcase_07 AC 4,980 ms
61,436 KB
testcase_08 AC 4,961 ms
60,476 KB
testcase_09 AC 4,965 ms
61,708 KB
testcase_10 AC 4,966 ms
61,008 KB
testcase_11 AC 4,971 ms
64,592 KB
testcase_12 AC 4,959 ms
60,644 KB
testcase_13 AC 4,959 ms
61,080 KB
testcase_14 AC 4,969 ms
61,156 KB
testcase_15 AC 4,965 ms
61,112 KB
testcase_16 AC 4,958 ms
61,012 KB
testcase_17 AC 4,952 ms
60,860 KB
testcase_18 AC 4,991 ms
60,312 KB
testcase_19 AC 4,964 ms
64,324 KB
testcase_20 AC 4,967 ms
64,008 KB
testcase_21 AC 4,976 ms
60,888 KB
testcase_22 AC 4,969 ms
60,868 KB
testcase_23 AC 4,965 ms
61,280 KB
testcase_24 AC 4,969 ms
62,292 KB
testcase_25 AC 4,968 ms
61,116 KB
testcase_26 AC 4,967 ms
61,268 KB
testcase_27 AC 4,964 ms
60,644 KB
testcase_28 AC 4,974 ms
62,464 KB
testcase_29 AC 4,978 ms
68,684 KB
testcase_30 AC 4,965 ms
61,476 KB
testcase_31 AC 4,974 ms
61,648 KB
testcase_32 AC 4,962 ms
60,992 KB
testcase_33 AC 4,970 ms
60,740 KB
testcase_34 AC 4,970 ms
61,132 KB
testcase_35 AC 4,972 ms
61,360 KB
testcase_36 AC 4,969 ms
70,208 KB
testcase_37 AC 4,968 ms
61,640 KB
testcase_38 AC 4,973 ms
60,700 KB
testcase_39 AC 4,970 ms
68,292 KB
testcase_40 AC 4,954 ms
61,284 KB
testcase_41 AC 4,968 ms
61,148 KB
testcase_42 AC 4,958 ms
60,460 KB
testcase_43 AC 4,965 ms
61,736 KB
testcase_44 AC 4,963 ms
61,572 KB
testcase_45 AC 4,979 ms
68,148 KB
testcase_46 AC 4,968 ms
69,488 KB
testcase_47 AC 4,965 ms
60,648 KB
testcase_48 AC 4,965 ms
60,712 KB
testcase_49 AC 4,970 ms
61,196 KB
testcase_50 AC 4,979 ms
62,232 KB
testcase_51 AC 4,966 ms
61,160 KB
testcase_52 AC 4,960 ms
60,916 KB
testcase_53 AC 4,981 ms
60,324 KB
testcase_54 AC 4,982 ms
61,132 KB
testcase_55 AC 4,967 ms
60,768 KB
testcase_56 AC 4,971 ms
70,572 KB
testcase_57 AC 4,979 ms
73,864 KB
testcase_58 AC 4,965 ms
60,692 KB
testcase_59 AC 4,968 ms
62,092 KB
testcase_60 AC 4,967 ms
63,260 KB
testcase_61 AC 4,978 ms
63,136 KB
testcase_62 AC 4,971 ms
60,732 KB
testcase_63 AC 4,966 ms
63,008 KB
testcase_64 AC 4,992 ms
62,348 KB
testcase_65 AC 4,973 ms
63,076 KB
testcase_66 AC 4,970 ms
61,072 KB
testcase_67 AC 4,964 ms
65,488 KB
testcase_68 AC 4,980 ms
63,400 KB
testcase_69 AC 4,977 ms
75,440 KB
testcase_70 AC 4,976 ms
74,424 KB
testcase_71 AC 4,991 ms
74,648 KB
testcase_72 AC 4,959 ms
65,920 KB
testcase_73 AC 4,970 ms
74,384 KB
testcase_74 AC 4,985 ms
65,936 KB
testcase_75 AC 4,968 ms
64,464 KB
testcase_76 AC 4,960 ms
64,140 KB
testcase_77 AC 4,972 ms
66,412 KB
testcase_78 AC 4,970 ms
74,284 KB
testcase_79 AC 4,965 ms
64,540 KB
testcase_80 AC 4,970 ms
67,080 KB
testcase_81 AC 4,977 ms
65,432 KB
testcase_82 AC 4,972 ms
68,320 KB
testcase_83 AC 4,967 ms
64,912 KB
testcase_84 AC 4,964 ms
68,484 KB
testcase_85 AC 4,972 ms
68,260 KB
testcase_86 AC 4,973 ms
71,508 KB
testcase_87 AC 4,970 ms
70,056 KB
testcase_88 AC 4,976 ms
68,060 KB
testcase_89 AC 4,978 ms
68,388 KB
testcase_90 AC 4,967 ms
68,068 KB
testcase_91 AC 4,970 ms
70,100 KB
testcase_92 AC 4,968 ms
68,796 KB
testcase_93 AC 4,979 ms
68,060 KB
testcase_94 AC 4,967 ms
70,816 KB
testcase_95 AC 4,976 ms
71,752 KB
testcase_96 AC 4,968 ms
70,860 KB
testcase_97 AC 4,966 ms
74,080 KB
testcase_98 AC 4,981 ms
72,276 KB
testcase_99 AC 4,971 ms
71,836 KB
testcase_100 AC 4,985 ms
77,472 KB
testcase_101 AC 4,970 ms
72,548 KB
testcase_102 AC 4,966 ms
70,316 KB
testcase_103 AC 4,962 ms
69,584 KB
testcase_104 AC 4,975 ms
70,524 KB
testcase_105 AC 4,964 ms
71,836 KB
testcase_106 AC 4,975 ms
70,448 KB
testcase_107 AC 4,990 ms
76,348 KB
testcase_108 AC 4,977 ms
72,192 KB
testcase_109 AC 4,969 ms
69,808 KB
testcase_110 AC 4,971 ms
74,336 KB
testcase_111 AC 4,987 ms
78,108 KB
testcase_112 AC 4,970 ms
74,252 KB
testcase_113 AC 4,972 ms
74,404 KB
testcase_114 AC 4,968 ms
76,544 KB
testcase_115 AC 4,976 ms
76,132 KB
testcase_116 AC 4,978 ms
75,620 KB
testcase_117 AC 4,976 ms
76,352 KB
testcase_118 AC 4,967 ms
74,620 KB
testcase_119 AC 4,983 ms
76,012 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