Submission #2432391


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        static final long MODULO = (long) 1e9 + 7;
        int n;
        int[] a;
        int[] w;

        int[] mul(int[] a, int[] b) {
            int[] c = new int[9];
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    long s = 0;
                    for (int k = 0; k < 3; ++k) s += a[(i) * 3 + k] * (long) b[(k) * 3 + j];
                    c[(i) * 3 + j] = (int) (s % MODULO);
                }
            }
            return c;
        }

        int[] add(int[] a, int[] b) {
            int[] c = new int[9];
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    long s = a[(i) * 3 + j] + b[(i) * 3 + j];
                    c[(i) * 3 + j] = (int) (s % MODULO);
                }
            }
            return c;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            n = in.nextInt();
            a = new int[n];
            for (int i = 0; i < n; ++i) a[i] = in.nextInt();
            for (int i = 0, j = n - 1; i < j; ++i, --j) {
                int t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
            int[] positions = new int[n];
            for (int i = 0; i < n; ++i) positions[i] = i;
            w = getW(a.clone());
            for (int x : w)
                if (x == 0) {
                    out.println(0);
                    return;
                }

            TaskE.Description desc = doit(0, n - 1, positions);
            out.println(desc.inside[(2) * 3 + 0]);
        }

        private int[] getW(int[] a) {
            Arrays.sort(a);
            int sofar = 0;
            int pos = a.length - 1;
            int[] w = new int[n];
            for (int i = n - 1; i >= 0; --i) {
                while (pos >= 0 && a[pos] == i + 1) {
                    ++sofar;
                    --pos;
                }
                w[i] = Math.max(0, sofar);
                --sofar;
            }
            return w;
        }

        private TaskE.Description doit(int min, int max, int[] positions) {
            TaskE.Description res = new TaskE.Description();
            if (min == max) {
                res.p00then01 = new int[positions.length][];
                res.p00then10 = new int[positions.length][];
                res.p01then11 = new int[positions.length][];
                res.p10then11 = new int[positions.length][];
                int k = min;
                for (int x : positions) if (a[x] != k + 1) throw new RuntimeException();
                res.p00 = new int[9];
                res.p00[(0) * 3 + 0] = w[k];
                res.p01 = new int[9];
                res.p01[(0) * 3 + 0] = w[k] - 1;
                res.p01[(1) * 3 + 0] = 1;
                res.p01[(1) * 3 + 1] = w[k];
                res.p10 = new int[9];
                res.p10[(0) * 3 + 0] = w[k] - 1;
                res.p11 = new int[9];
                res.p11[(0) * 3 + 0] = Math.max(0, w[k] - 2);
                res.p11[(1) * 3 + 0] = 1;
                res.p11[(1) * 3 + 1] = w[k] - 1;
                res.p11[(2) * 3 + 1] = 1;
                res.p11[(2) * 3 + 2] = w[k];
                for (int i = 0; i < positions.length; ++i) {
                    res.p01then11[i] = res.p11;
                    res.p10then11[i] = res.p11;
                    res.p00then10[i] = res.p10;
                    res.p00then01[i] = res.p01;
                }
                int[] sum = new int[9];
                res.inside = new int[9];
                for (int x : positions) {
                    res.inside = add(res.inside, sum);
                    sum = add(sum, res.p11);
                }
            } else {
                int mid = (min + max) / 2;
                int numBottom = 0;
                int numTop = 0;
                for (int x : positions)
                    if (a[x] <= mid + 1) {
                        ++numBottom;
                    } else {
                        ++numTop;
                    }
                int[] bottomPositions = new int[numBottom];
                int[] topPositions = new int[numTop];
                numBottom = 0;
                numTop = 0;
                for (int x : positions)
                    if (a[x] <= mid + 1) {
                        bottomPositions[numBottom++] = x;
                    } else {
                        topPositions[numTop++] = x;
                    }
                TaskE.Description bottom;
                TaskE.Description top;
                if (numBottom < numTop) {
                    bottom = doit(min, mid, bottomPositions);
                    top = doit(mid + 1, max, topPositions);
                } else {
                    top = doit(mid + 1, max, topPositions);
                    bottom = doit(min, mid, bottomPositions);
                }
                res.p00then01 = new int[positions.length][];
                res.p00then10 = new int[positions.length][];
                res.p01then11 = new int[positions.length][];
                res.p10then11 = new int[positions.length][];
                res.inside = add(mul(bottom.inside, top.p00), mul(bottom.p11, top.inside));
                res.p00 = mul(bottom.p00, top.p00);
                res.p11 = mul(bottom.p11, top.p11);
                res.p01 = mul(bottom.p01, top.p01);
                res.p10 = mul(bottom.p10, top.p10);
                int topPtr = 0;
                int bottomPtr = 0;
                int[] topSum = new int[9];
                int[] bottomSum = new int[9];
                int ptr = 0;
                for (int x : positions)
                    if (a[x] <= mid + 1) {
                        res.p00then01[ptr] = mul(bottom.p00then01[bottomPtr], top.p00);
                        res.p00then10[ptr] = mul(bottom.p00then10[bottomPtr], top.p00);
                        res.p10then11[ptr] = mul(bottom.p10then11[bottomPtr], top.p10);
                        res.p01then11[ptr] = mul(bottom.p01then11[bottomPtr], top.p01);
                        res.inside = add(res.inside, mul(bottom.p10then11[bottomPtr], topSum));
                        bottomSum = add(bottomSum, bottom.p01then11[bottomPtr]);
                        ++bottomPtr;
                        ++ptr;
                    } else {
                        res.p00then01[ptr] = mul(bottom.p01, top.p00then01[topPtr]);
                        res.p00then10[ptr] = mul(bottom.p10, top.p00then10[topPtr]);
                        res.p10then11[ptr] = mul(bottom.p11, top.p10then11[topPtr]);
                        res.p01then11[ptr] = mul(bottom.p11, top.p01then11[topPtr]);
                        res.inside = add(res.inside, mul(bottomSum, top.p00then01[topPtr]));
                        topSum = add(topSum, top.p00then10[topPtr]);
                        ++topPtr;
                        ++ptr;
                    }
            }
            return res;
        }

        static class Description {
            int[] inside;
            int[] p00;
            int[] p11;
            int[] p01;
            int[] p10;
            public int[][] p10then11;
            public int[][] p01then11;
            public int[][] p00then01;
            public int[][] p00then10;

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

Submission Info

Submission Time
Task E - Inversions
User Petr
Language Java7 (OpenJDK 1.7.0)
Score 0
Code Size 9030 Byte
Status MLE
Exec Time 1945 ms
Memory 427456 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1700
Status
AC × 4
AC × 20
MLE × 23
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt
Case Name Status Exec Time Memory
sample_01.txt AC 76 ms 18644 KB
sample_02.txt AC 76 ms 18644 KB
sample_03.txt AC 76 ms 18644 KB
sample_04.txt AC 80 ms 18900 KB
subtask_1_01.txt AC 77 ms 20820 KB
subtask_1_02.txt AC 77 ms 18772 KB
subtask_1_03.txt AC 321 ms 43576 KB
subtask_1_04.txt AC 183 ms 23704 KB
subtask_1_05.txt AC 838 ms 155280 KB
subtask_1_06.txt MLE 1403 ms 396984 KB
subtask_1_07.txt AC 935 ms 175164 KB
subtask_1_08.txt AC 835 ms 194240 KB
subtask_1_09.txt MLE 1321 ms 355268 KB
subtask_1_10.txt AC 487 ms 108308 KB
subtask_1_11.txt MLE 1190 ms 288652 KB
subtask_1_12.txt MLE 1286 ms 300848 KB
subtask_1_13.txt MLE 1529 ms 380140 KB
subtask_1_14.txt AC 417 ms 43000 KB
subtask_1_15.txt AC 354 ms 44052 KB
subtask_1_16.txt MLE 1729 ms 408824 KB
subtask_1_17.txt MLE 1874 ms 427456 KB
subtask_1_18.txt MLE 1699 ms 422744 KB
subtask_1_19.txt MLE 1795 ms 419412 KB
subtask_1_20.txt MLE 1705 ms 420456 KB
subtask_1_21.txt MLE 1671 ms 417216 KB
subtask_1_22.txt MLE 1810 ms 424464 KB
subtask_1_23.txt MLE 1793 ms 414420 KB
subtask_1_24.txt MLE 1904 ms 418672 KB
subtask_1_25.txt AC 326 ms 43916 KB
subtask_1_26.txt AC 342 ms 44268 KB
subtask_1_27.txt MLE 1845 ms 424124 KB
subtask_1_28.txt MLE 1945 ms 422800 KB
subtask_1_29.txt MLE 1685 ms 424636 KB
subtask_1_30.txt MLE 1768 ms 418928 KB
subtask_1_31.txt MLE 1658 ms 416040 KB
subtask_1_32.txt MLE 1647 ms 417916 KB
subtask_1_33.txt MLE 1833 ms 403988 KB
subtask_1_34.txt MLE 1809 ms 419588 KB
subtask_1_35.txt MLE 1816 ms 416732 KB