[백준] 2776 암기왕

새싹·2021년 9월 16일
0

알고리즘

목록 보기
9/49

📌 문제 링크

2776 암기왕

💡 문제 풀이

책에 있는 부품 찾기 문제랑 비슷하다
전통적인 이진탐색 개념대로 입력받은 수를 정렬하고 찾으면 된다

여기서는 수첩 2에 적혀있는 숫자의 순서를 지키면서 수첩 1에 있는지 확인하면 되니까 수첩1 숫자들만 정렬하고 풀었다

📋 코드

# 2776 암기왕
import sys


def binary_search(start, end, target):
    while start <= end:
        mid = (start + end) // 2
        # 숫자를 찾았을 때
        if note1[mid] == target:
            return mid
        elif note1[mid] > target:
            end = mid - 1
        else:
            start = mid + 1
    return None


t = int(input())

for i in range(t):
    n = int(input())
    note1 = list(map(int, sys.stdin.readline().split()))
    m = int(input())
    note2 = list(map(int, sys.stdin.readline().split()))

    note1.sort()

    for x in note2:
        # 숫자가 수첩1에 있는지 확인
        result = binary_search(0, n-1, x)
        if result is not None:
            print("1")
        else:
            print("0")

0개의 댓글