Submission #24365098


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.util.Random;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            DIncDecDecomposition solver = new DIncDecDecomposition();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DIncDecDecomposition {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.ri();
            long[] A = in.rl(n);
            LongLinearFunction[] B = new LongLinearFunction[n];
            LongLinearFunction[] C = new LongLinearFunction[n];
            B[0] = new LongLinearFunction(1, 0);
            C[0] = new LongLinearFunction(-1, A[0]);
            for (int i = 1; i < n; i++) {
                B[i] = B[i - 1].clone();
                C[i] = C[i - 1].clone();
                if (A[i] > A[i - 1]) {
                    B[i].b += A[i] - A[i - 1];
                } else {
                    C[i].b += A[i] - A[i - 1];
                }
            }
            long[] x = new long[2 * n];
            for (int i = 0; i < n; i++) {
                x[i] = B[i].b * B[i].a;
                x[i + n] = C[i].b * C[i].a;
            }
            Randomized.shuffle(x);
            Arrays.sort(x);
            long choice = x[n];
            long ans = 0;
            for (long z : x) {
                ans += Math.abs(choice - z);
            }
            out.println(ans);
        }

    }

    static class Randomized {
        public static void shuffle(long[] data) {
            shuffle(data, 0, data.length);
        }

        public static void shuffle(long[] data, int from, int to) {
            to--;
            for (int i = from; i <= to; i++) {
                int s = nextInt(i, to);
                long tmp = data[i];
                data[i] = data[s];
                data[s] = tmp;
            }
        }

        public static int nextInt(int l, int r) {
            return RandomWrapper.INSTANCE.nextInt(l, r);
        }

    }

    static abstract class CloneSupportObject<T> implements Cloneable {
        public T clone() {
            try {
                return (T) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    static class RandomWrapper {
        private Random random;
        public static final RandomWrapper INSTANCE = new RandomWrapper();

        public RandomWrapper() {
            this(new Random());
        }

        public RandomWrapper(Random random) {
            this.random = random;
        }

        public RandomWrapper(long seed) {
            this(new Random(seed));
        }

        public int nextInt(int l, int r) {
            return random.nextInt(r - l + 1) + l;
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private static final int THRESHOLD = 32 << 10;
        private OutputStream writer;
        private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
        private static Field stringBuilderValueField;
        private char[] charBuf = new char[THRESHOLD * 2];
        private byte[] byteBuf = new byte[THRESHOLD * 2];

        static {
            try {
                stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
                stringBuilderValueField.setAccessible(true);
            } catch (Exception e) {
                stringBuilderValueField = null;
            }
            stringBuilderValueField = null;
        }

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        private void afterWrite() {
            if (cache.length() < THRESHOLD) {
                return;
            }
            flush();
        }

        public FastOutput(OutputStream writer) {
            this.writer = writer;
        }

        public FastOutput append(char c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(long c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput println(long c) {
            return append(c).println();
        }

        public FastOutput println() {
            return append('\n');
        }

        public FastOutput flush() {
            try {
                if (stringBuilderValueField != null) {
                    try {
                        byte[] value = (byte[]) stringBuilderValueField.get(cache);
                        writer.write(value, 0, cache.length());
                    } catch (Exception e) {
                        stringBuilderValueField = null;
                    }
                }
                if (stringBuilderValueField == null) {
                    int n = cache.length();
                    if (n > byteBuf.length) {
                        //slow
                        writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
//                writer.append(cache);
                    } else {
                        cache.getChars(0, n, charBuf, 0);
                        for (int i = 0; i < n; i++) {
                            byteBuf[i] = (byte) charBuf[i];
                        }
                        writer.write(byteBuf, 0, n);
                    }
                }
                writer.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                writer.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class LongLinearFunction extends CloneSupportObject<LongLinearFunction> {
        public long a;
        public long b;

        public LongLinearFunction(long a, long b) {
            this.a = a;
            this.b = b;
        }

        public LongLinearFunction(long b) {
            this(0, b);
        }

        public String toString() {
            return String.format("%dx+%d", a, b);
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        public void populate(long[] data) {
            for (int i = 0; i < data.length; i++) {
                data[i] = readLong();
            }
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int ri() {
            return readInt();
        }

        public long[] rl(int n) {
            long[] ans = new long[n];
            populate(ans);
            return ans;
        }

        public int readInt() {
            boolean rev = false;

            skipBlank();
            if (next == '+' || next == '-') {
                rev = next == '-';
                next = read();
            }

            int val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 - next + '0';
                next = read();
            }

            return rev ? val : -val;
        }

        public long readLong() {
            boolean rev = false;

            skipBlank();
            if (next == '+' || next == '-') {
                rev = next == '-';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 - next + '0';
                next = read();
            }

            return rev ? val : -val;
        }

    }
}

Submission Info

Submission Time
Task D - Inc, Dec - Decomposition
User daltao
Language Java (OpenJDK 11.0.6)
Score 700
Code Size 9724 Byte
Status AC
Exec Time 318 ms
Memory 70660 KiB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 40
Set Name Test Cases
Sample 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt
All 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt, 02_small_01.txt, 02_small_02.txt, 02_small_03.txt, 02_small_04.txt, 02_small_05.txt, 02_small_06.txt, 02_small_07.txt, 02_small_08.txt, 02_small_09.txt, 02_small_10.txt, 03_rand_01.txt, 03_rand_02.txt, 03_rand_03.txt, 03_rand_04.txt, 03_rand_05.txt, 03_rand_06.txt, 03_rand_07.txt, 03_rand_08.txt, 03_rand_09.txt, 03_rand_10.txt, 03_rand_11.txt, 03_rand_12.txt, 03_rand_13.txt, 03_rand_14.txt, 03_rand_15.txt, 03_rand_16.txt, 03_rand_17.txt, 03_rand_18.txt, 03_rand_19.txt, 03_rand_20.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt, 04_handmade_05.txt, 04_handmade_06.txt, 04_handmade_07.txt
Case Name Status Exec Time Memory
01_sample_01.txt AC 80 ms 33956 KiB
01_sample_02.txt AC 74 ms 33888 KiB
01_sample_03.txt AC 81 ms 33924 KiB
02_small_01.txt AC 82 ms 34128 KiB
02_small_02.txt AC 81 ms 34092 KiB
02_small_03.txt AC 83 ms 34024 KiB
02_small_04.txt AC 76 ms 34136 KiB
02_small_05.txt AC 77 ms 33872 KiB
02_small_06.txt AC 81 ms 33772 KiB
02_small_07.txt AC 75 ms 34104 KiB
02_small_08.txt AC 78 ms 33732 KiB
02_small_09.txt AC 77 ms 34052 KiB
02_small_10.txt AC 83 ms 34024 KiB
03_rand_01.txt AC 119 ms 36444 KiB
03_rand_02.txt AC 198 ms 45328 KiB
03_rand_03.txt AC 207 ms 47204 KiB
03_rand_04.txt AC 114 ms 36544 KiB
03_rand_05.txt AC 236 ms 47748 KiB
03_rand_06.txt AC 126 ms 37400 KiB
03_rand_07.txt AC 187 ms 44080 KiB
03_rand_08.txt AC 257 ms 51072 KiB
03_rand_09.txt AC 275 ms 65288 KiB
03_rand_10.txt AC 287 ms 67672 KiB
03_rand_11.txt AC 129 ms 39492 KiB
03_rand_12.txt AC 177 ms 43240 KiB
03_rand_13.txt AC 204 ms 44596 KiB
03_rand_14.txt AC 139 ms 40364 KiB
03_rand_15.txt AC 197 ms 44068 KiB
03_rand_16.txt AC 257 ms 52896 KiB
03_rand_17.txt AC 144 ms 39356 KiB
03_rand_18.txt AC 276 ms 65860 KiB
03_rand_19.txt AC 113 ms 36072 KiB
03_rand_20.txt AC 305 ms 69196 KiB
04_handmade_01.txt AC 318 ms 70660 KiB
04_handmade_02.txt AC 318 ms 70424 KiB
04_handmade_03.txt AC 187 ms 66224 KiB
04_handmade_04.txt AC 222 ms 68404 KiB
04_handmade_05.txt AC 226 ms 67952 KiB
04_handmade_06.txt AC 264 ms 67732 KiB
04_handmade_07.txt AC 274 ms 69780 KiB