๐ฅ์ฒซ ์๋ (์๊ฐ์ด๊ณผ)
์ด๋ถํ์์ ํ๊ธด ํ๋๋ฐ.. 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()
'๐๊ณต๋ถ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ์ฌ๋ฐ๋ฅธ ๊ดํธ (0) | 2020.11.30 |
---|---|
[๋ฐฑ์ค] 2110๋ฒ ๊ณต์ ๊ธฐ ์ค์น (0) | 2020.11.24 |
[๋ฐฑ์ค] 2805๋ฒ ๋๋ฌด ์๋ฅด๊ธฐ (0) | 2020.11.24 |
[๋ฐฑ์ค] 1654๋ฒ ๋์ ์๋ฅด๊ธฐ (0) | 2020.11.20 |
[๋ฐฑ์ค] 4573 ๋ฒ ์ ํ๋๋ฒ (0) | 2020.11.19 |
๋๊ธ