#19 백준 1920번 파이썬
우보만리
- binary search 알고리즘을 활용하는 문제
- 블로그를 보고 공부를 했다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 이진 탐색 알고리즘 | |
def binary_search(arr, target, low, high): | |
mid = (high + low)//2 | |
if high >= low: | |
if arr[mid] == target: | |
return 1 | |
elif arr[mid] > target: | |
return binary_search(arr, target, low, mid - 1) | |
else: | |
return binary_search(arr, target, mid+1, high) | |
else: | |
return 0 | |
N = int(input()) | |
A = sorted(list(map(int, input().split()))) | |
M = int(input()) | |
candidate = list(map(int,input().split())) | |
for m in candidate: | |
print(binary_search(A,m,0, len(A)-1)) | |
## Run time : 720 ms | |
## Memory : 44992 KB |