[이분탐색]
사전에 숫자 개수 먼저 구해 놓기 - 아이디어 참고
import sys
from collections import defaultdict
input=sys.stdin.readline
N=int(input())
arr=list(map(int,input().split()))
M=int(input())
brr=list(map(int,input().split()))
count=defaultdict(int)
for a in arr:
count[a]+=1
arr.sort()
for b in brr:
left,right=0,N-1
answer=0
while left<=right:
mid=(left+right)//2
if arr[mid]==b:
answer=count[b]
break
if arr[mid]>b:
right=mid-1
else:
left=mid+1
print(answer,end=" ")
https://www.acmicpc.net/problem/10816
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
'IT > coding study' 카테고리의 다른 글
[acmicpc] 6593. 상범 빌딩(python) (0) | 2023.02.07 |
---|---|
[acmicpc] 20057. 마법사 상어와 토네이도(python) (0) | 2023.02.05 |
[acmicpc] 2206. 벽 부수고 이동하기(python) (0) | 2022.10.10 |
[acmicpc] 21921. 블로그 (python) (0) | 2022.10.04 |
[acmicpc] 17822. 원판 돌리기 (python) (1) | 2022.09.30 |