목차

불만 정렬

ps
링크acmicpc.net/…
출처BOJ
문제 번호5012
문제명불만 정렬
레벨플래티넘 3
분류

Inversion Counting

시간복잡도O(nlogn)
인풋사이즈n <= 100,000
사용한 언어Python
제출기록48536KB / 1108ms
최고기록1108ms
해결날짜2021/04/07

풀이

코드

"""Solution code for "BOJ 5012. 불만 정렬"

- Problem link: https://www.acmicpc.net/problem/5012
- Solution link: http://www.teferi.net/ps/problems/boj/5012
"""

from teflib import fenwicktree


def main():
    N = int(input())
    a = [int(x) for x in input().split()]

    nums = fenwicktree.OrderStatisticTree(N)
    inversions = fenwicktree.OrderStatisticTree(N)
    answer = 0
    for a_i in reversed(a):
        answer += inversions.count_less_than(a_i)
        inversion_count = nums.count_less_than(a_i)
        inversions.add(a_i, inversion_count)
        nums.add(a_i, 1)

    print(answer)


if __name__ == '__main__':
    main()