-
10815 숫자 카드알고리즘/BOJ 2024. 3. 30. 22:23
https://www.acmicpc.net/problem/10815
10815번: 숫자 카드
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
아이디어
1. python find 사용시 시간초과
2. 이분탐색으로 시간복잡도 줄여야 통과
if __name__ == '__main__': N = int(input()) cards = list(map(int, input().split(" "))) M = int(input()) find = list(map(int, input().split(" "))) res = [] sorted_cards = sorted(cards) for f in find: low = 0 high = len(sorted_cards) - 1 while True: if low > high: res.append("0") break mid = (low + high) // 2 if f == sorted_cards[mid]: res.append("1") break elif f > sorted_cards[mid]: low = mid + 1 else: high = mid - 1 print(" ".join(res))
'알고리즘 > BOJ' 카테고리의 다른 글
12051 가장 긴 증가하는 부분 수열2 (0) 2024.03.31 2110 공유기 설치 (0) 2024.03.30 2805번 아기 상어 (0) 2024.03.28 10816 숫자 카드 2 (0) 2024.03.26 [BOJ]17142번 연구소3 (0) 2020.08.17