10816๋ฒ: ์ซ์ ์นด๋ 2
์ฒซ์งธ ์ค์ ์๊ทผ์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ซ์ ์นด๋์ ๊ฐ์ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ์ซ์ ์นด๋์ ์ ํ์๋ ์ ์๊ฐ ์ฃผ์ด์ง๋ค. ์ซ์ ์นด๋์ ์ ํ์๋ ์๋ -10,000,000๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 10,
www.acmicpc.net
๋ฌธ์
์ซ์ ์นด๋๋ ์ ์ ํ๋๊ฐ ์ ํ์ ธ ์๋ ์นด๋์ด๋ค. ์๊ทผ์ด๋ ์ซ์ ์นด๋ N๊ฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ์ ์ M๊ฐ๊ฐ ์ฃผ์ด์ก์ ๋, ์ด ์๊ฐ ์ ํ์๋ ์ซ์ ์นด๋๋ฅผ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋์ง ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์๊ทผ์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ซ์ ์นด๋์ ๊ฐ์ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ์ซ์ ์นด๋์ ์ ํ์๋ ์ ์๊ฐ ์ฃผ์ด์ง๋ค. ์ซ์ ์นด๋์ ์ ํ์๋ ์๋ -10,000,000๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 10,000,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ ์งธ ์ค์๋ M(1 ≤ M ≤ 500,000)์ด ์ฃผ์ด์ง๋ค. ๋ท์งธ ์ค์๋ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋ ์ซ์ ์นด๋์ธ์ง ๊ตฌํด์ผ ํ M๊ฐ์ ์ ์๊ฐ ์ฃผ์ด์ง๋ฉฐ, ์ด ์๋ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด์ ธ ์๋ค. ์ด ์๋ -10,000,000๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 10,000,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง M๊ฐ์ ์์ ๋ํด์, ๊ฐ ์๊ฐ ์ ํ ์ซ์ ์นด๋๋ฅผ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋์ง๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ
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 2
ํ์ด
- ์ด๋ถํ์ ์ด์ฉ
Solution
์๊ฐ์ด๊ณผ ํ์ด
M = int(input())
MCard = list(map(int, input().split()))
N = int(input())
NCard = list(map(int, input().split()))
MCard.sort() #sort O(nlogn)
r = []
def Binary_Search(x):
start = 0
end = M - 1
num = 0
while start <= end:
mid = (start + end) // 2
if MCard[mid] == x:
#num += 1
#MCard.remove(x)
return MCard.count(x) #count O(n^2)
if MCard[mid] < x:
start = mid + 1
else:
end = mid - 1
return 0
for i in NCard:
r.append(Binary_Search(i))
print(r)
์ณ์ ํ์ด 1 (Counter ์ด์ฉ)
from sys import stdin
from collections import Counter #collections - ์ปจํ
์ด๋ ๋ฐ์ดํฐํ
M = stdin.readline()
MCard = sorted(map(int, stdin.readline().split()))
N = stdin.readline()
NCard = map(int, stdin.readline().split())
C = Counter(MCard) #Counter({10: 3, -10: 2, 3: 2, 2: 1, 6: 1, 7: 1})
print(' '.join(f'{C[m]}' if m in C else '0' for m in NCard))
์ณ์ ํ์ด 2 (Dict ์ด์ฉ)
from sys import stdin
N = stdin.readline()
NCard = sorted(map(int, stdin.readline().split()))
M = stdin.readline()
MCard = map(int, stdin.readline().split())
def Binary_Search(x, NCard, start, end):
if start > end:
return 0
mid = (start + end) // 2
if NCard[mid] == x:
return cnt.get(x) #dict.get(key, default = None)
elif NCard[mid] > x:
return Binary_Search(x, NCard, start, mid - 1)
else:
return Binary_Search(x, NCard, mid + 1, end)
cnt = { } #dictionary
for i in NCard:
if i in cnt:
cnt[i] += 1
else:
cnt[i] = 1
for i in MCard:
print(Binary_Search(i, NCard, 0, len(NCard)-1), end = ' ')'๐งฉ Algorithm > [BOJ] Silver' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| BOJ 11441๋ฒ : ํฉ ๊ตฌํ๊ธฐ (Python/Silver 3) (0) | 2023.03.22 |
|---|---|
| BOJ 11659๋ฒ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 4 (Python/Silver 3) (0) | 2023.03.22 |
| BOJ 16401๋ฒ : ๊ณผ์ ๋๋ ์ฃผ๊ธฐ (Python/Silver 2) (0) | 2023.03.20 |
| BOJ 11399๋ฒ : ATM (Python/Silver 4) (0) | 2023.03.02 |
| BOJ 11047๋ฒ : ๋์ 0 (Python/Silver 4) (0) | 2023.02.19 |