10816번 – 숫자카드2

1

문제

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

풀이

코드

from sys import stdin

def intJoin(arr): return " ".join([str(i) for i in arr])

def groupping(arr):
  standard, retArr, count = (arr[0], [], 1)
  i = 1
  while(i < len(arr)):
    left, right = (arr[i-1], arr[i])
    if left != right:
      retArr.append((left, count))
      count = 1
    else: count += 1
    i += 1
  if count > 0:
    retArr.append((arr[-1], count))
  return retArr
      

def answer():
  global n, m, nArr, mArr
  nArr = groupping(sorted(nArr))
  retArr = []
  for i in mArr:
    start, end, isIncluded = (0, len(nArr)-1, 0)
    while(start <= end):
      mid = (start+end)//2
      num, count = nArr[mid]
      if i == num:
        isIncluded = count
        break
      elif num < i: start = mid+1
      elif num > i: end = mid-1
    retArr.append(isIncluded)
  print(intJoin(retArr))
  
  
n = int(input())
nArr = list(map(int, stdin.readline().split()))
m = int(input())
mArr = list(map(int, stdin.readline().split()))
answer()

Add Comment