DevYoon

[๋ฐฑ์ค€] 1920. ์ˆ˜ ์ฐพ๊ธฐ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 1920. ์ˆ˜ ์ฐพ๊ธฐ (Python)

gimewn 2022. 10. 28. 00:47

link ๐Ÿ”— https://www.acmicpc.net/problem/1920

 

1920๋ฒˆ: ์ˆ˜ ์ฐพ๊ธฐ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค

www.acmicpc.net

 

โœ๏ธ ์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ

โœ๏ธ ๋ถ„๋ช… while๋ฌธ์ด ๋๋‚˜์•ผ ํ•˜๋Š”๋ฐ ์ž๊พธ ๋‹ต์ด ์•ˆ ๋‚˜์˜ค๊ณ  ๋Œ์•„๊ฐ€์„œ ๋‹นํ™ฉํ–ˆ๋‹ค. ์ฐพ์•˜์„ ๊ฒฝ์šฐ answer๋ฅผ 1๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  break๋ฅผ ์•ˆ ์จ์ค€ ๊ฒŒ ์›์ธ์ด์—ˆ๋‹ค๐Ÿ˜…

 

N = int(input())
N_num = list(map(int, input().split()))

M = int(input())
M_num = list(map(int, input().split()))

N_num.sort()

def binary_search(ground, target):
    s = 0
    e = len(ground) - 1
    answer = 0

    while s <= e:
        mid = (s+e) // 2
        if ground[mid] > target:
            e = mid - 1
        elif ground[mid] < target:
            s = mid + 1
        else:
            answer = 1
            break

    return answer

for num in M_num:
    print(binary_search(N_num, num))