๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€] 10816๋ฒˆ ์ˆซ์ž ์นด๋“œ2

by rindev 2020. 11. 22.

www.acmicpc.net/problem/10816

 

10816๋ฒˆ: ์ˆซ์ž ์นด๋“œ 2

์ฒซ์งธ ์ค„์— ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ˆ˜๋Š” -10,000,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,

www.acmicpc.net

 

 

๐Ÿ”ฅ์ฒซ ์‹œ๋„ (์‹œ๊ฐ„์ดˆ๊ณผ)

์ด๋ถ„ํƒ์ƒ‰์„ ํ•˜๊ธด ํ•˜๋Š”๋ฐ.. start์™€ end๋ฅผ ์ฐพ์•„์„œ end-start+1 ํ•ด์„œ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ๊ธฐ๋กœ ํ•จ

๊ทผ๋ฐ while 2๊ฐœ๋ฅผ ๋Œ์•„์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ์ธ๊ฒƒ๊ฐ™๋‹ค...ใ… ใ…  

๋”๋ณด๊ธฐ
# 10816๋ฒˆ : ์ˆซ์ž์นด๋“œ
def binary_search_10816():
  _ = int(input())
  N = list(map(int, input().split()))
    
  _ = int(input())
  M = list(map(int, input().split()))

  # N = [6, 3, 2, 10, 10, 10, -10, -10, 7, 3]
  # M = [10, 9, -5, 2, 3, 4, 5, -10]

            #   0   1   2  3  4  5  6  7   8   9
  N.sort() # [-10, -10, 2, 3, 3, 6, 7, 10, 10, 10]
  # print(N)

  setN = set(N)

  result_arr = []

  def find_start(s,e,m):
    start = 0
    while 1:
      c = (e+s) // 2

      if m == N[c] :
        if N[c-1] == N[c]:
          e = c - 1
        elif N[c-1] != N[c]:
          start = c
          break 
      elif m < N[c]:
        e = c - 1
      elif m > N[c]:
        s = c + 1

      if s > e:
        start = e
        break
    return start

  def find_end(s,e,m):
    end = 0
    while 1:
      c = (e+s) // 2

      if m == N[c] :
        if N[c+1] == N[c]:
          s = c + 1
        elif N[c+1] != N[c]:
          end = c
          break 
      elif m < N[c]:
        e = c - 1
      elif m > N[c]:
        s = c + 1

      if s >= e:
        end = e
        break
    return end

  for m in M:
    s = 0
    e = len(N)-1 
    # print(m)

    if m not in setN:
      result_arr.append(0)
      continue

    st = find_start(s,e,m)
    ed = find_end(s,e,m)

    result_arr.append(ed-st+1)
  print(' '.join(str(r) for r in result_arr))   

 

์‹œ๊ฐ„์ดˆ๊ณผ

 

 

๐Ÿ”ฅ๋‘๋ฒˆ์งธ ์‹œ๋„ (์‹œ๊ฐ„์ดˆ๊ณผ)

์œ„์—์„œ ์กฐ๊ธˆ๋งŒ ๊ณ ์ณ์„œ(๊ฑฐ์˜ ๊ฐ™์Œ) ๋˜ ์‹œ๊ฐ„์ดˆ๊ณผใ… ใ…  ์ด๋ถ„ํƒ์ƒ‰ ๊ผญ ์จ์•ผํ•˜๋‚˜์š” ์•ˆ์“ธ๋ž˜ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… 

์•”ํŠผ ๊ทธ๋ž˜์„œ ์ฝ”๋“œ ์•ˆ์˜ฌ๋ฆผ.. ๋น„์Šทํ•ด์„œ....

 

 

๐Ÿ”ฅ์„ธ๋ฒˆ์งธ ์‹œ๋„ (ํ†ต๊ณผใ… ใ… )

๊ทผ๋ฐ ์ด๋ถ„ํƒ์ƒ‰์„ ๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค dict๋กœ ํ’€์—ˆ์–ด์š”... 

๋”๋ณด๊ธฐ
# 10816๋ฒˆ : ์ˆซ์ž์นด๋“œ 2ํŠธ
def binary_search_10816_2():
  _ = int(input())
  N = list(map(int, input().split()))
    
  _ = int(input())
  M = list(map(int, input().split()))

  # N = [6, 2, 2, 2, 3, 2, 10, 10, 10, 10, 10, -10, -10, 7, 3]
  # M = [10, 9, -5, 2, 3, 4, 5, -10]

            #   0   1   2  3  4  5  6  7   8   9
  N.sort() # [-10, -10, 2, 3, 3, 6, 7, 10, 10, 10]
  # print(N)

  dictN = {}

  for n in N:
    if n in dictN:
      dictN[n] += 1
    else:
      dictN[n] = 1
  
  # print(dictN)
  
  for m in M:
    if m not in dictN:
      print(0, end=' ')
    else:
      print(dictN.get(m), end=' ')


binary_search_10816_2()

 

๋Œ“๊ธ€