結果

問題 No.696 square1001 and Permutation 5
ユーザー ardggyardggy
提出日時 2021-05-23 04:26:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,367 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 27,624 KB
最終ジャッジ日時 2024-04-19 11:52:01
合計ジャッジ時間 22,907 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 sys
sys.setrecursionlimit(1000000)
def debug(*args, **kwd):
    import os
    if os.getenv('DEBUG'): print(*args, **kwd, file=sys.stderr)

def input():
    return sys.stdin.readline()[:-1]

def int0(s: str) -> int:
    return int(s) - 1

class Fenwick:
    __slots__ = ["size", "tree"]

    def __init__(self, size):
        self.size = size
        self.tree = [0] * (size + 1)

    def sum(self, i):
        s = 0
        i += 1
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

from math import factorial as f
def main(_=0):
    N = int(input())
    P = [int(x) for x in input().split()]

    fenwick = Fenwick(N)
    k, n = 1, N
    for a in P:
        a -= 1
        x = a - fenwick.sum(a)
        fenwick.add(a, 1)
        k += x * f(n-1)
        n -= 1

    print(k)

def as_input(s: str) -> None:
    import io
    global input
    f = io.StringIO(s)
    input = lambda: f.readline().rstrip()
    return None

sample1 = """3
2 3 1
"""
sample2 = """5
3 1 5 4 2
"""
sample3 = """5
1 4 3 2 5
"""
def test():
    """
    >>> main(as_input(sample1))
    4
    >>> main(as_input(sample2))
    54
    >>> main(as_input(sample3))
    15
    """
    pass

if __name__ == '__main__':
    main()
0