https://www.acmicpc.net/problem/1057
1057번: 토너먼트
김지민은 N명이 참가하는 스타 토너먼트에 진출했다. 토너먼트는 다음과 같이 진행된다. 일단 N명의 참가자는 번호가 1번부터 N번까지 배정받는다. 그러고 난 후에 서로 인접한 번호끼리 스타를
www.acmicpc.net
풀이
문제를 보았을때 알수있는 힌트
토너먼트가 반씩 줄어든다는 생각을 가지고 풀면 해결되는문제
라운드의 개수를 미리 파악하고 전체 토너먼트 경기를 반씩 줄어가면서 찾는 방식을 택함
import sys
input = sys.stdin.readline
n, kim,im = map(int,input().split())
max_n=1
while True:
if n<=2**max_n:
n=2**max_n
break
else:
max_n+=1
n_list=[i for i in range(1,n+1)]
count=-1
# 반으로 나누어서 보기
def tournament(n_list,n):
global count
count+=1
if kim in n_list[:n//2] and im in n_list[:n//2]:
# print(n_list[:n//2])
tournament(n_list[:n//2],n//2)
elif kim in n_list[n//2:] and im in n_list[n//2:]:
# print(n_list[n//2:])
tournament(n_list[n//2:],n//2)
else:
return
tournament(n_list,n)
# print(max_n,n,count)
print(max_n-count)
어렵지 않게 내맘대로 코드 짜봤지만 찾아보니 더 쉽게 코드 짠게있어서 참고하여 적어 놓았다.
n,start,end=map(int, input().split())
cnt=0
while start!=end:
start -= start//2
end -= end//2
cnt+=1
print(cnt)
내가 짠 코드보다 12ms 더 빠른것으로 나오며 메모리도 덜 먹는것을 볼수있었다.
좀더 생각하면서 효율적으로 짤방법을 생각해봐야할듯하다.
https://github.com/dydwkd486/coding_test/blob/main/baekjoon/baekjoon1057.py
GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리
코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.
github.com
'Coding Test > baekjoon' 카테고리의 다른 글
[백준 11055] 가장 큰 증가 부분 수열- python (solved.ac - 실버 2) (0) | 2022.02.09 |
---|---|
[백준 1015] 수열 정렬- python (solved.ac - 실버 4) (0) | 2022.01.25 |
[백준 2941] 크로아티아 알파벳 - python (solved.ac - 실버 5) (0) | 2022.01.22 |
[백준 2581] 소수 - python (solved.ac - 실버 5) (0) | 2022.01.21 |
[백준 1182] 부분수열의 합 - python (solved.ac - 실버 2) (0) | 2022.01.19 |