From 61fc46563d55b315b52d83924984f081c5f301a4 Mon Sep 17 00:00:00 2001 From: SSUM <116950962+ssum21@users.noreply.github.com> Date: Wed, 29 Jan 2025 16:09:50 +0900 Subject: [PATCH] =?UTF-8?q?[Platinum=20V]=20Title:=20=EB=B2=84=EB=B8=94=20?= =?UTF-8?q?=EC=86=8C=ED=8A=B8,=20Time:=202508=20ms,=20Memory:=2087856=20KB?= =?UTF-8?q?=20-BaekjoonHub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 백준/Platinum/1517. 버블 소트/README.md | 30 +++++++++++++++ 백준/Platinum/1517. 버블 소트/버블 소트.py | 44 ++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 백준/Platinum/1517. 버블 소트/README.md create mode 100644 백준/Platinum/1517. 버블 소트/버블 소트.py diff --git a/백준/Platinum/1517. 버블 소트/README.md b/백준/Platinum/1517. 버블 소트/README.md new file mode 100644 index 0000000..212c0fe --- /dev/null +++ b/백준/Platinum/1517. 버블 소트/README.md @@ -0,0 +1,30 @@ +# [Platinum V] 버블 소트 - 1517 + +[문제 링크](https://www.acmicpc.net/problem/1517) + +### 성능 요약 + +메모리: 87856 KB, 시간: 2508 ms + +### 분류 + +자료 구조, 분할 정복, 세그먼트 트리, 정렬 + +### 제출 일자 + +2025년 1월 29일 16:09:29 + +### 문제 설명 + +

N개의 수로 이루어진 수열 A[1], A[2], …, A[N]이 있다. 이 수열에 대해서 버블 소트를 수행할 때, Swap이 총 몇 번 발생하는지 알아내는 프로그램을 작성하시오.

+ +

버블 소트는 서로 인접해 있는 두 수를 바꿔가며 정렬하는 방법이다. 예를 들어 수열이 3 2 1 이었다고 하자. 이 경우에는 인접해 있는 3, 2가 바뀌어야 하므로 2 3 1 이 된다. 다음으로는 3, 1이 바뀌어야 하므로 2 1 3 이 된다. 다음에는 2, 1이 바뀌어야 하므로 1 2 3 이 된다. 그러면 더 이상 바꿔야 할 경우가 없으므로 정렬이 완료된다.

+ +### 입력 + +

첫째 줄에 N(1 ≤ N ≤ 500,000)이 주어진다. 다음 줄에는 N개의 정수로 A[1], A[2], …, A[N]이 주어진다. 각각의 A[i]는 0 ≤ |A[i]| ≤ 1,000,000,000의 범위에 들어있다.

+ +### 출력 + +

첫째 줄에 Swap 횟수를 출력한다

+ diff --git a/백준/Platinum/1517. 버블 소트/버블 소트.py b/백준/Platinum/1517. 버블 소트/버블 소트.py new file mode 100644 index 0000000..f80fcb5 --- /dev/null +++ b/백준/Platinum/1517. 버블 소트/버블 소트.py @@ -0,0 +1,44 @@ +import sys + +N = int(sys.stdin.readline().rstrip()) +arr = list(map(int, input().split(' '))) +arr.insert(0, 0) +tarr = [0] * (N+1) +num=0 + + +def ms(s, e): + global num + if(e-s<1): + return + m = int(s + (e-s)/2) + ms(s, m) + ms(m+1, e) + for i in range(s, e+1): + tarr[i] = arr[i] + k=s + index1 = s + index2 = m+1 + while index1<=m and index2 <=e : + if tarr[index1] > tarr[index2]: + arr[k]=tarr[index2] + num = num + index2 - k + k += 1 + index2+=1 + else: + arr[k]=tarr[index1] + k += 1 + index1 += 1 + while index1<=m: + arr[k]=tarr[index1] + k += 1 + index1 += 1 + while index2<=e: + arr[k]=tarr[index2] + k+=1 + index2+=1 + +ms(1, N) + +print(num) +