[python] 백준 10816번 숫자 카드 2

Youngseo Lee·2024년 8월 15일

자료구조

목록 보기
4/7

백준 10816번 숫자 카드 2

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

문제

풀이

나는 이 문제를 bisect를 통한 이분 탐색, 그리고 해시 맵을 사용해서 풀어봤다.

input 받기 (nlist, mlist)

n = int(input())
nlist = list(map(int, input().split()))
nlist.sort()
m = int(input())
mlist = list(map(int, input().split()))

dict

dictt = {}
for i in nlist: 
    if i in dictt:
        dictt[i] += 1
    else:
        dictt[i] = 1

for i in mlist:
    if i in dictt:
        print(dictt[i], end=' ')
    else:
        print(0, end=' ')

bisect

from bisect import bisect_left, bisect_right
for i in mlist: 
    a = bisect_left(nlist, i)
    b = bisect_right(nlist, i)
    print(b - a, end = ' ')

📌 주의

bisect 는 같은 수가 여러 번 출력될 때 아주 유용한 듯하다. 같은 수가 몇 개 있는 지 궁금하면 bisect_right 에서 bisect_left 를 빼주면 된다.

profile
leenthepotato

0개의 댓글