Skip to content

Commit 4bcae1f

Browse files
committed
[Silver III] Title: 진수 정렬 (Easy), Time: 48 ms, Memory: 32412 KB -BaekjoonHub
1 parent 35c083a commit 4bcae1f

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# [Silver III] 진수 정렬 (Easy) - 32283
2+
3+
[문제 링크](https://www.acmicpc.net/problem/32283)
4+
5+
### 성능 요약
6+
7+
메모리: 32412 KB, 시간: 48 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 정렬
12+
13+
### 제출 일자
14+
15+
2025년 6월 16일 21:09:33
16+
17+
### 문제 설명
18+
19+
<p>한별이는 길이 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>의 이진수를 모두 모은 뒤 특별한 방식으로 정렬하여 사용하려 한다.</p>
20+
21+
<p>정렬 기준은 다음과 같다.</p>
22+
23+
<ul>
24+
<li>이진수 내의 1의 개수로 오름차순 정렬한다.</li>
25+
<li>1의 개수가 같다면 이진수를 뒤집었을 때의 오름차순으로 정렬한다.</li>
26+
</ul>
27+
28+
<p>예를 들어, <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>이 3일 때 이진수는 다음 순서로 정렬된다.</p>
29+
30+
<ul>
31+
<li>000</li>
32+
<li>100</li>
33+
<li>010</li>
34+
<li>001</li>
35+
<li>110</li>
36+
<li>101</li>
37+
<li>011</li>
38+
<li>111</li>
39+
</ul>
40+
41+
<p>어느날 친구 선린이가 찾아와 길이가 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>인 이진수 하나를 주며 몇 번째 위치에 있는지 알려달라고 했다. 하지만 한별이는 다른 일이 많아 아직 이진수들을 정렬하지 못했다. 한별이를 도와주자!</p>
42+
43+
### 입력
44+
45+
<p>첫째 줄에 이진수의 길이 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>이 주어진다.</p>
46+
47+
<p>둘째 줄에 길이 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>의 이진수 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D446 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$S$</span></mjx-container>가 주어진다.</p>
48+
49+
<p>이진수는 0으로 시작할 수 있다.</p>
50+
51+
### 출력
52+
53+
<p>주어진 이진수와 길이가 같은 모든 이진수를 정렬하였을 때, 주어진 이진수는 몇 번째 위치에 있는지 출력한다.</p>
54+
55+
<p>위치는 <mjx-container class="MathJax" jax="CHTML" style="font-size: 104.6%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c30"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>0</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$0$</span></mjx-container>번부터 시작한다.</p>
56+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 32283 진수 정렬 Easy
2+
# 실버 3
3+
4+
import sys
5+
input = sys.stdin.readline
6+
7+
from itertools import combinations
8+
9+
N = int(input())
10+
S = input().strip()
11+
12+
# S의 1의 위치 인덱스를 가진 리스트 -> 튜플(조합이랑 비교하기 위함)
13+
S_list = [i+1 for i, c in enumerate(S) if c == '1']
14+
S_tuple = tuple(S_list)
15+
16+
# 전체
17+
count = 0
18+
19+
# one = 0,1,2,…,N 개의 1 그룹을 차례로 훑으면서
20+
for one in range(0, N+1):
21+
combs = list(combinations(range(1, N+1), one))
22+
23+
# 1의 개수가 같다면 이진수를 뒤집었을 때의 오름차순으로 정렬
24+
# 뒤집은 비트 문자열(s[::-1])의 오름차순으로 정렬
25+
combs.sort(key=lambda comb:
26+
''.join('1' if idx in comb else '0' for idx in range(N, 0, -1))
27+
)
28+
29+
if S_tuple in combs:
30+
# 같은 1개수 그룹 내에서의 0-based 인덱스만큼 더하고 출력
31+
count += combs.index(S_tuple)
32+
print(count)
33+
break
34+
else:
35+
# 아직 찾기 전이니 이 그룹 전체 개수만큼 건너뛰기
36+
count += len(combs)

0 commit comments

Comments
 (0)