-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Silver IV] Title: 수 찾기, Time: 416 ms, Memory: 43640 KB -BaekjoonHub
- Loading branch information
1 parent
85c81f7
commit 6185249
Showing
2 changed files
with
23 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,31 @@ | ||
|
||
""" | ||
[눈여겨볼 것] | ||
Q. 왜 이진탐색을 써야하는가? | ||
A. N=10^5, O(N^2)의 경우 100억 연산, 즉 100초가 걸린다. | ||
이에 O(N*logN)으로, 탐색 연산수를 줄여줘야한다. | ||
[고도화 필요] | ||
- 이진트리구조를 이용하고 싶었으나, 실패 | ||
- 우선 분할 정복으로 풀이 | ||
""" | ||
|
||
import sys | ||
def input(): | ||
return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
A = list(map(int, input().split())) | ||
A.sort() | ||
# 팁) 대소 규칙을 넣어, 좌/우 편하게 버리기 위함 | ||
# - 순간 A 기존 순서가 바뀌는 단점이 있다고 생각했는데, B를 순회하니깐 노 상관 | ||
nums = list(map(int, input().split())) | ||
nums.sort() | ||
|
||
M = int(input()) | ||
B = list(map(int, input().split())) | ||
targets = map(int, input().split()) | ||
|
||
|
||
def binary_search(target, start_idx, end_idx): | ||
if start_idx == end_idx: | ||
if A[start_idx] == target: | ||
return True | ||
else: | ||
return False | ||
def check_is_in(target: int): | ||
l = 0 | ||
r = len(nums) - 1 | ||
|
||
while l <= r: | ||
mid = (l + r) // 2 | ||
|
||
mid_idx = (start_idx + end_idx) // 2 | ||
mid_item = A[mid_idx] | ||
if nums[mid] == target: | ||
return 1 | ||
elif nums[mid] < target: | ||
l = mid + 1 | ||
else: | ||
r = mid - 1 | ||
|
||
if target <= mid_item: # 좌 탐색 | ||
return binary_search(target, start_idx, mid_idx) | ||
else: # 우 탐색 | ||
return binary_search(target, mid_idx + 1, end_idx) | ||
return 0 | ||
|
||
|
||
size = len(A) | ||
for item in B: | ||
if binary_search(item, 0, size - 1): | ||
print(1) | ||
else: | ||
print(0) | ||
for target in targets: # O(10^5) | ||
print(check_is_in(target)) # O(log(10^5)) 으로 줄여야함 |