DevYoon

[๋ฐฑ์ค€] 2805. ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 2805. ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ (Python)

gimewn 2022. 10. 26. 23:35

link ๐Ÿ”— https://www.acmicpc.net/problem/2805

 

2805๋ฒˆ: ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

์ฒซ์งธ ์ค„์— ๋‚˜๋ฌด์˜ ์ˆ˜ N๊ณผ ์ƒ๊ทผ์ด๊ฐ€ ์ง‘์œผ๋กœ ๊ฐ€์ ธ๊ฐ€๋ ค๊ณ  ํ•˜๋Š” ๋‚˜๋ฌด์˜ ๊ธธ์ด M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) ๋‘˜์งธ ์ค„์—๋Š” ๋‚˜๋ฌด์˜ ๋†’์ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‚˜๋ฌด์˜ ๋†’์ด์˜ ํ•ฉ์€ ํ•ญ์ƒ M๋ณด

www.acmicpc.net

 

โœ๏ธ ์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ!

โœ๏ธ ์ด๋ถ„ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ฐ€์žฅ ์ ์ ˆํ•œ ์ ˆ๋‹จ๊ธฐ ๋†’์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

โœ๏ธ ๋‚˜๋ฌด์˜ ๋†’์ด๊ฐ€ 0๋ถ€ํ„ฐ 1000000000 ์‚ฌ์ด์ด๋ฏ€๋กœ, s๋ฅผ 0, e๋ฅผ ๊ฐ€์žฅ ๋†’์€ ๋‚˜๋ฌด์˜ ๋†’์ด๋กœ ๋‘์—ˆ๋‹ค.
โœ๏ธ ์ด๋ถ„ํƒ์ƒ‰์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ๋ณต์Šตํ•˜๊ธฐ์— ์ข‹์€ ๋ฌธ์ œ์˜€๋‹ค.

 

N, M = map(int, input().split())
trees = list(map(int, input().split()))

# ๋‚˜๋ฌด์˜ ๋†’์ด๋Š” 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์–‘์˜ ์ •์ˆ˜ ๋˜๋Š” 0 => s๋Š” 0, e๋Š” ๊ฐ€์žฅ ๋†’์€ ๋‚˜๋ฌด์˜ ๋†’์ด
s = 0
e = max(trees)

# ์ด๋ถ„ ํƒ์ƒ‰
while s <= e:
    mid = (s+e)//2
    # ์ž˜๋ผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋‚˜๋ฌด์˜ ๊ธธ์ด ํ•ฉ ๊ตฌํ•˜๊ธฐ
    temp = sum(tree-mid for tree in trees if tree-mid>0)
    # ๋‚˜๋ฌด๋ฅผ ๋” ๋งŽ์ด ์ž˜๋ผ๋ฒ„๋ ธ๋‹ค๋ฉด ์ ˆ๋‹จ๊ธฐ ๋†’์ด๋ฅผ ๋†’์—ฌ์•ผ ํ•˜๋ฏ€๋กœ s๋ฅผ mid+1๋กœ ์˜ฎ๊ฒจ์คŒ
    if temp >= M:
        s = mid+1
    # ๋‚˜๋ฌด๋ฅผ ๋œ ์ž˜๋ž๋‹ค๋ฉด ์ ˆ๋‹จ๊ธฐ ๋†’์ด๋ฅผ ๋‚ฎ์ถฐ์•ผ ํ•˜๋ฏ€๋กœ e๋ฅผ mid-1๋กœ ์˜ฎ๊ฒจ์คŒ
    else:
        e = mid-1

print(e)