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