๋๋ฌด๋ฅผ ์์๋ฅด๋ ์๋ค์..
์ํผ ๋๋ฌด๋ฅผ ์๋ฅด๋๊ฑธ... ๋๊ธฐ๋กํฉ๋๋ค
๐โ๏ธ์์ฑํ ์ฝ๋
๋๋ณด๊ธฐ
# 2805 ๋ฒ : ๋๋ฌด ์๋ฅด๊ธฐ
def binary_search_2805():
n, m = map(int, input().split())
trees = list(map(int, input().split()))
# n = 4
# m = 7
# trees = [20, 15, 10, 17]
s = 0
e = max(trees)
answer = 0
while s <= e :
c = (s+e) // 2
# print('s,e,c====', s,e,c)
hap = 0
for t in trees:
if t >= c:
tmp = t - c
hap += tmp
# print('tmp, hap', tmp, hap)
if hap < m : #๊ธธ์ด๊ฐ ๋ชจ์๋
e = c - 1
elif hap >= m : #๊ธธ์ด๊ฐ ๋์นจ
s = c + 1
answer = c
print(answer)
์ผ๋จ s,c,e๋ ๋ค์๊ณผ ๊ฐ๋ค(start, center, end์ ์ฝ์...)
๊ทธ๋์ ๊ฐ์ ธ๊ฐ๋ ๋๋ฌด์ ํฉ(hap)์ด ๋ด๊ฐ ํ์ํ ๊ธธ์ด m๋ณด๋ค ์์ผ๋ฉด(๋ชจ์๋ผ๋ฉด)
e = c -1 ๋ก ๋ณ๊ฒฝํ์ฌ H๋ฅผ ๋ด๋ฆฐ๋ค. -> ๋๋ฌด๋ฅผ ๋ ์๋ฅด๊ฒ ๋จ
hap์ด m๋ณด๋ค ํฌ๋ค๋ฉด (ํ์ํ ๊ธธ์ด๋ณด๋ค ๋ง์ด ์๋ฆ)
s = c+1 ๋ก ๋ณ๊ฒฝํ์ฌ H๋ฅผ ์ฌ๋ฆฐ๋ค. ->๋๋ฌด๋ฅผ ๋ ์๋ฅด๊ฒ ๋จ
์ด๋ฅผ ๋ฐ๋ณตํ๋ฉด์ ์ ์ ํ ๊ธธ์ด๋ฅผ ์ฐพ๋๋ค.
'๐๊ณต๋ถ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ์ฌ๋ฐ๋ฅธ ๊ดํธ (0) | 2020.11.30 |
---|---|
[๋ฐฑ์ค] 2110๋ฒ ๊ณต์ ๊ธฐ ์ค์น (0) | 2020.11.24 |
[๋ฐฑ์ค] 10816๋ฒ ์ซ์ ์นด๋2 (0) | 2020.11.22 |
[๋ฐฑ์ค] 1654๋ฒ ๋์ ์๋ฅด๊ธฐ (0) | 2020.11.20 |
[๋ฐฑ์ค] 4573 ๋ฒ ์ ํ๋๋ฒ (0) | 2020.11.19 |
๋๊ธ