์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 1920๋ฒˆ : ์ˆ˜ ์ฐพ๊ธฐ

  • -

๋ฌธ์ œ ๋งํฌ : 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๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค์ด A์•ˆ๏ฟฝ๏ฟฝ

www.acmicpc.net

 

 

๋‘๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํƒ์ƒ‰ํ•  ๋Œ€์ƒ์ด๊ณ , ๋‘๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๊ฐ€ target ์ด๋‹ค.

์ด๋ฅผ for๋ฌธ์„ ํ†ตํ•ด ๊ฐ๊ฐ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

์ด์ง„ํƒ์ƒ‰ , ์ด๋ถ„ํƒ์ƒ‰

https://infinitt.tistory.com/286

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ) ์ด์ง„ ํƒ์ƒ‰, ์ด๋ถ„ ํƒ์ƒ‰ (Binary serach) _ python ์žฌ๊ท€

* ์ด์ง„ ํƒ์ƒ‰ (Binary serach) ๊ฐœ๋… ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์— ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ˆœ์ฐจํƒ์ƒ‰(linear search)์€ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฐพ์„ ๋Œ€์ƒ์ด ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ๊ฒ€์ƒ‰ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด์ง„ ํƒ์ƒ‰์€ ๏ฟฝ๏ฟฝ

infinitt.tistory.com

*ํŒŒ์ด์ฌ ์ฝ”๋“œ
N = int(input())
arr_n = list(map(int,input().split()))
arr_n.sort()
M = int(input())
arr_m = list(map(int,input().split()))





for i in arr_m:
    start = 0
    end = N-1
    target = i
    answer = 0
    while (start <= end):
        mid = (start+end) //2
        if arr_n[mid] == target:
            answer = 1
            break
        elif target < arr_n[mid]:
            end = mid-1
        else:
            start = mid+1
    print(answer)

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.