ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 1517 |
문제명 | 버블 소트 |
레벨 | 골드 1 |
분류 |
Inversion Counting |
시간복잡도 | O(nlogn) |
인풋사이즈 | n<=500,000 |
사용한 언어 | Python |
제출기록 | 86980KB / 2804ms |
최고기록 | 2644ms |
해결날짜 | 2021/05/26 |
태그 |
"""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()
"""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()