코딩테스트/BOJ

[백준] [이분탐색] 10816. 숫자 카드 2

박소민 2023. 4. 27. 21:26
10816. 숫자 카드 2
 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

 

 

  • 내 풀이
    • 실버4인데 반례를 못찾아서 몇일 걸린 문제.....;;
    • check에 중복이 들어오는데 연속되지 않고 같은 수가 따로따로 들어올 수 있음
      • -> 같은 값 불러오려면 딕셔너리 인덱스로 값 찾아야함
  • 반례
10
6 3 2 10 10 10 -10 -10 7 3
8
10 9 -5 2 3 4 5 10

#결과
3 0 0 1 2 0 0 3
import sys
input=sys.stdin.readline
n=int(input())
lst=list(map(int,input().split()))
m=int(input())
check=list(map(int,input().split()))
result=[0]*m


# -10 -10 2 3 3 6 7 10 10 10
def bin(start, end, i):
    x=check[i]
    if start>end:
        return

    mid=(start+end)//2
    if lst[mid]==x:
        result[i]=lst[start:end+1].count(x)
        return

    if x>lst[mid]:
        bin(mid+1, end, i)
    else:
        bin(start, mid-1, i)

find={}
lst.sort()
for i,c in enumerate(check):
    if c in find:
        result[i]=find[c]
        continue
    bin(0,n-1,i)
    find[c]=result[i]

for r in result:
  print(str(r), end=" ")