結果

問題 No.1604 Swap Sort:ONE
ユーザー 👑 tamatotamato
提出日時 2021-07-16 21:29:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,154 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,700 KB
実行使用メモリ 77,040 KB
最終ジャッジ日時 2023-09-20 13:03:26
合計ジャッジ時間 3,632 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,268 KB
testcase_01 AC 73 ms
71,616 KB
testcase_02 AC 73 ms
71,244 KB
testcase_03 AC 73 ms
71,628 KB
testcase_04 AC 77 ms
71,608 KB
testcase_05 AC 85 ms
76,472 KB
testcase_06 AC 84 ms
77,040 KB
testcase_07 AC 85 ms
76,824 KB
testcase_08 AC 83 ms
76,800 KB
testcase_09 AC 86 ms
76,952 KB
testcase_10 AC 84 ms
76,560 KB
testcase_11 AC 86 ms
76,604 KB
testcase_12 AC 86 ms
77,040 KB
testcase_13 AC 86 ms
76,976 KB
testcase_14 AC 86 ms
76,544 KB
testcase_15 AC 86 ms
76,684 KB
testcase_16 AC 85 ms
76,592 KB
testcase_17 AC 84 ms
77,004 KB
testcase_18 AC 87 ms
76,676 KB
testcase_19 AC 86 ms
76,608 KB
testcase_20 AC 82 ms
76,820 KB
testcase_21 AC 85 ms
76,828 KB
testcase_22 AC 84 ms
76,636 KB
testcase_23 AC 80 ms
75,820 KB
testcase_24 AC 80 ms
76,068 KB
testcase_25 AC 85 ms
76,680 KB
testcase_26 AC 85 ms
76,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

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

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

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

    N = int(input())
    P = list(map(int, input().split()))

    bit = Bit(N+1)
    idx = [0] * (N+1)
    for i, p in enumerate(P):
        idx[p] = i+1

    ans = 0
    for p in range(N, 0, -1):
        i = idx[p]
        ans += bit.sum(i)
        bit.add(i, 1)
    print(ans)


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