[Gold II] Title: 버블 소트, Time: 1112 ms, Memory: 106812 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-01-28 21:15:07 +09:00
parent 0077886f8d
commit fa6d5abe88
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
# [Gold II] 버블 소트 - 1377
[문제 링크](https://www.acmicpc.net/problem/1377)
### 성능 요약
메모리: 106812 KB, 시간: 1112 ms
### 분류
정렬
### 제출 일자
2025년 1월 28일 21:13:25
### 문제 설명
<p>버블 소트 알고리즘을 다음과 같이 C++로 작성했다.</p>
<pre>bool changed = false;
for (int i=1; i<=N+1; i++) {
changed = false;
for (int j=1; j<=N-i; j++) {
if (A[j] > A[j+1]) {
changed = true;
swap(A[j], A[j+1]);
}
}
if (changed == false) {
cout << i << '\n';
break;
}
}
</pre>
<p>위 소스에서 N은 배열의 크기이고, A는 정렬해야 하는 배열이다. 배열은 A[1]부터 사용한다.</p>
<p>위와 같은 소스를 실행시켰을 때, 어떤 값이 출력되는지 구해보자.</p>
### 입력
<p>첫째 줄에 N이 주어진다. N은 500,000보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 A[1]부터 A[N]까지 하나씩 주어진다. A에 들어있는 수는 1,000,000보다 작거나 같은 자연수 또는 0이다.</p>
### 출력
<p>정답을 출력한다.</p>

View File

@@ -0,0 +1,17 @@
import sys
input = sys.stdin.readline().rstrip()
arr = []
temp = -500001
n = int(input)
for i in range(n):
num = int(sys.stdin.readline().rstrip())
arr.append((num, i))
arr=sorted(arr)
for i in range(n):
temp = max(temp, arr[i][1] - i)
print(temp+1)