IT/coding study

[acmicpc] 10816. 숫자 카드 2(python)

seyeonHello 2022. 11. 11. 00:47

[이분탐색]

사전에 숫자 개수 먼저 구해 놓기 - 아이디어 참고

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