목차

버블 소트

ps
링크acmicpc.net/…
출처BOJ
문제 번호1517
문제명버블 소트
레벨골드 1
분류

Inversion Counting

시간복잡도O(nlogn)
인풋사이즈n<=500,000
사용한 언어Python
제출기록86980KB / 2804ms
최고기록2644ms
해결날짜2021/05/26
태그

38단계

풀이

코드

코드 1 - 머지소트

"""Solution code for "BOJ 1517. 버블 소트".

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


def sort_and_count_inversion(list_):
    if len(list_) <= 1:
        return 0
    mid = len(list_) // 2
    left, right = list_[:mid], list_[mid:]
    inversion_count = (sort_and_count_inversion(left)
                       + sort_and_count_inversion(right))
    l_size, r_size = mid, len(right)
    i = j = 0
    ret = []
    while True:
        try:
            if left[i] <= right[j]:
                ret.append(left[i])
                i += 1
            else:
                ret.append(right[j])
                j += 1
                inversion_count += l_size - i
        except IndexError:
            break            
    list_[:] = ret
    list_.extend(left[i:])
    list_.extend(right[j:])
    return inversion_count


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

    print(sort_and_count_inversion(A))


if __name__ == '__main__':
    main()

코드 2 - Order Statistic Tree

"""Solution code for "BOJ 1517. 버블 소트".

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

from teflib import fenwicktree


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    
    compress_map = {x: i for i, x in enumerate(sorted(set(A)))}
    comp_a = [compress_map[x] for x in A]
    ost = fenwicktree.OrderStatisticTree(len(compress_map))
    answer = 0
    for a_i in reversed(comp_a):
        answer += ost.count_less_than(a_i)
        ost.add(a_i)
        
    print(answer)


if __name__ == '__main__':
    main()