문제 링크
10815번: 숫자 카드
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
풀이
import sys
n = int(sys.stdin.readline())
n_lst = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
m_lst = list(map(int, sys.stdin.readline().split()))
def binary_search(lst, n):
low = 0
high = len(lst)-1
while low <= high:
mid = (low+high)//2
if n > lst[mid]:
low = mid+1
elif n < lst[mid]:
high = mid-1
else:
return 1
return 0
n_lst.sort()
answer = []
for i in m_lst:
answer.append(binary_search(n_lst, i))
print(" ".join(map(str, answer)))
이진 탐색 활용하여 해결하였다.
'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 10814] 나이순 정렬 with Node.js (0) | 2021.04.10 |
---|---|
[백준 11650] 좌표 정렬하기 with Node.js (0) | 2021.04.10 |
[백준 7568] 덩치 with Node.js (0) | 2021.04.09 |
[백준 1181] 단어 정렬 with Node.js (0) | 2021.04.09 |
[백준 11866] 요세푸스 문제 0 with Python (0) | 2021.04.09 |