๐Ÿ‹ ๋ฌธ์ œ๋งํฌ


https://www.acmicpc.net/problem/2776

๐Ÿ‰ Code


์ œ์ถœ ๋‚ ์งœ

2021/04/23

๋ฉ”๋ชจ๋ฆฌ

336536 KB

์‹œ๊ฐ„

1368 ms

์ด๋ถ„ํƒ์ƒ‰ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  set ์œผ๋กœ ํ’ˆ!

โ†’ set ์œผ๋กœ ์•ˆํ•˜๊ณ  list๋กœ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ ๋œธ

T = int(input())
for _ in range(T):
    n = int(input())
    note1 = set(map(int, input().split()))
    m = int(input())
    note2 = list(map(int, input().split()))
    for num in note2:
        if num in note1:
            print(1)
        else:
            print(0)

์ œ์ถœ ๋‚ ์งœ

2021/04/23

๋ฉ”๋ชจ๋ฆฌ

345464 KB

์‹œ๊ฐ„

2036 ms

์ด๋ถ„ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ ํ’€๊ธฐ!

โ†’ ๊ทผ๋ฐ ์‹œ๊ฐ„์ด ๋”.. ์˜ค๋ž˜ ๊ฑธ๋ฆผ....?

def binary_search(start, end, num):
    global note1
    while start <= end:
        mid = (start + end) // 2
        if note1[mid] == num:
            return 1
        elif note1[mid] < num:
            start = mid + 1
        else:
            end = mid - 1
    return 0

T = int(input())

for _ in range(T):
    n = int(input())
    note1 = list(map(int, input().split()))
    m = int(input())
    note2 = list(map(int, input().split()))

    note1.sort()

    for num in note2:
        print(binary_search(0, n-1, num))