結果

問題 No.696 square1001 and Permutation 5
ユーザー しらっ亭しらっ亭
提出日時 2018-06-09 00:11:11
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,484 bytes
コンパイル時間 3,109 ms
コンパイル使用メモリ 75,664 KB
実行使用メモリ 93,748 KB
最終ジャッジ日時 2023-09-13 00:46:59
合計ジャッジ時間 25,543 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Sol {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = scanner.nextInt();
        }

        BinaryIndexedTree ft = new BinaryIndexedTree(n);

        long[] factSum = new long[n];

        for (int i = 0; i < n - 1; i++) {
            int a = A[i];
            int cnt = a-1-ft.sum(a);
            int len = n - i;
            factSum[len-1] += cnt;
            ft.add(a, 1);
        }
//        System.out.println(Arrays.toString(factSum));

        BigInteger ans = BigInteger.ONE;
        BigInteger fact = BigInteger.ONE;

        for (int i = 1; i < n; i++) {
            fact = fact.multiply(BigInteger.valueOf(i));
            if (factSum[i] > 0) {
                ans = ans.add(fact.multiply(BigInteger.valueOf(factSum[i])));
            }
        }

        System.out.println(ans);
    }
}

class BinaryIndexedTree {
    private int n;
    private int[] val;

    public BinaryIndexedTree(int n) {
        this.n = n;
        val = new int[n];
    }

    public void add(int i, int x) {
        for (; i < n; i |= i + 1) val[i] += x;
    }

    int sum(int i) {
        int s = 0;
        --i;
        if (i >= n) i = n - 1;
        for (; i >= 0; i = (i & (i + 1)) - 1) s += val[i];
        return s;
    }
}
0