Problem Solving/BOJ & Programmers

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

geum 2024. 9. 19. 17:34

โœ๏ธ ๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ๋‚˜๋ฌด M๋ฏธํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ทผ์ฒ˜์— ๋‚˜๋ฌด๋ฅผ ๊ตฌ์ž…ํ•  ๊ณณ์ด ๋ชจ๋‘ ๋งํ•ด๋ฒ„๋ ธ๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ถ€์— ๋ฒŒ๋ชฉ ํ—ˆ๊ฐ€๋ฅผ ์š”์ฒญํ–ˆ๋‹ค. ์ •๋ถ€๋Š” ์ƒ๊ทผ์ด๋„ค ์ง‘ ๊ทผ์ฒ˜์˜ ๋‚˜๋ฌด ํ•œ ์ค„์— ๋Œ€ํ•œ ๋ฒŒ๋ชฉ ํ—ˆ๊ฐ€๋ฅผ ๋‚ด์ฃผ์—ˆ๊ณ , ์ƒ๊ทผ์ด๋Š” ์ƒˆ๋กœ ๊ตฌ์ž…ํ•œ ๋ชฉ์žฌ์ ˆ๋‹จ๊ธฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋‚˜๋ฌด๋ฅผ ๊ตฌํ•  ๊ฒƒ์ด๋‹ค.

๋ชฉ์žฌ์ ˆ๋‹จ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค. ๋จผ์ €, ์ƒ๊ทผ์ด๋Š” ์ ˆ๋‹จ๊ธฐ์— ๋†’์ด H๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋†’์ด๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ†ฑ๋‚ ์ด ๋•…์œผ๋กœ๋ถ€ํ„ฐ H๋ฏธํ„ฐ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค. ๊ทธ ๋‹ค์Œ, ํ•œ ์ค„์— ์—ฐ์†ํ•ด์žˆ๋Š” ๋‚˜๋ฌด๋ฅผ ๋ชจ๋‘ ์ ˆ๋‹จํ•ด๋ฒ„๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ, ๋†’์ด๊ฐ€ H๋ณด๋‹ค ํฐ ๋‚˜๋ฌด๋Š” H ์œ„์˜ ๋ถ€๋ถ„์ด ์ž˜๋ฆด ๊ฒƒ์ด๊ณ , ๋‚ฎ์€ ๋‚˜๋ฌด๋Š” ์ž˜๋ฆฌ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ ์ค„์— ์—ฐ์†ํ•ด์žˆ๋Š” ๋‚˜๋ฌด์˜ ๋†’์ด๊ฐ€ 20, 15, 10, 17์ด๋ผ๊ณ  ํ•˜์ž. ์ƒ๊ทผ์ด๊ฐ€ ๋†’์ด๋ฅผ 15๋กœ ์ง€์ •ํ–ˆ๋‹ค๋ฉด, ๋‚˜๋ฌด๋ฅผ ์ž๋ฅธ ๋’ค์˜ ๋†’์ด๋Š” 15, 15, 10, 15๊ฐ€ ๋  ๊ฒƒ์ด๊ณ , ์ƒ๊ทผ์ด๋Š” ๊ธธ์ด๊ฐ€ 5์ธ ๋‚˜๋ฌด์™€ 2์ธ ๋‚˜๋ฌด๋ฅผ ๋“ค๊ณ  ์ง‘์— ๊ฐˆ ๊ฒƒ์ด๋‹ค. (์ด 7๋ฏธํ„ฐ๋ฅผ ์ง‘์— ๋“ค๊ณ  ๊ฐ„๋‹ค) ์ ˆ๋‹จ๊ธฐ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋†’์ด๋Š” ์–‘์˜ ์ •์ˆ˜ ๋˜๋Š” 0์ด๋‹ค.

์ƒ๊ทผ์ด๋Š” ํ™˜๊ฒฝ์— ๋งค์šฐ ๊ด€์‹ฌ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ๋‚˜๋ฌด๋ฅผ ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์ง‘์œผ๋กœ ๊ฐ€์ ธ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ, ์ ์–ด๋„ M๋ฏธํ„ฐ์˜ ๋‚˜๋ฌด๋ฅผ ์ง‘์— ๊ฐ€์ ธ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ ์ ˆ๋‹จ๊ธฐ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋†’์ด์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿค– ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๐Ÿง ๋‚œ์ด๋„/์†Œ์š” ์‹œ๊ฐ„

  • ๋‚œ์ด๋„: solved.ac ๊ธฐ์ค€ S2
  • ์†Œ์š” ์‹œ๊ฐ„: 19๋ถ„ → ์–ด์ œ ๋žœ์„  ์ž๋ฅด๊ธฐ๋ฅผ ํ’€๊ณ  ๋ด์„œ ๊ทธ๋ ‡์ง€ ์ด ๋ฌธ์ œ๋ฅผ ๋จผ์ € ๋ดค๋‹ค๋ฉด ์ธก์ • ๋ถˆ๊ฐ€์˜€์„ ๋“ฏ

 

๐Ÿ’ก ํ’€์ด

์–ด์ œ ์˜ฌ๋ฆฐ '๋žœ์„  ์ž๋ฅด๊ธฐ'์™€ ๋™์ผํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ์ƒ‰ ์œ ํ˜•์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ฝ”๋“œ ํ‹€์€ ๊ทธ๋Œ€๋กœ ์“ฐ๊ณ  ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

(1) ์ฝ”๋“œ by ๋‚˜

import sys

input = sys.stdin.readline

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

tree_heights = list(map(int, input().split()))

start, end = 1, max(tree_heights)

while start <= end:
    mid = (start+end)//2
    
    machine_height = 0
    
    for tree in tree_heights:
        if tree >= mid:
            machine_height += (tree-mid)
    
    if machine_height >= M:
        start = mid+1
    else:
        end = mid-1

print(end)

 

Line 17: ์ž˜๋ฆฌ๊ณ  ๋‚จ์€ ๋‚˜๋ฌด์˜ ๊ธธ์ด๊ฐ€ 0 ๋ฏธ๋งŒ์ผ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋ฌด ๋†’์ด๊ฐ€ mid๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋งŒ ์ฒ˜๋ฆฌํ•ด์„œ ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ ธ๊ฐ€๋Š” ๋‚˜๋ฌด์— ๋”ํ•ด์คฌ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์—†์œผ๋‹ˆ๊นŒ ์˜ˆ์ œ ๋‹ต์ด ์ด์ƒํ•˜๊ฒŒ ๋‚˜์™”์Œ

 

๊ทผ๋ฐ ์ด ์ฝ”๋“œ๋กœ ํ†ต๊ณผ๋Š” ํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋ฌด๋ ค 4224ms๋‚˜ ๊ฑธ๋ ค์„œ ์ฑ—์ง€ํ”ผํ‹ฐ๊ฐ€ ์•Œ๋ ค์ค€ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด๋ณด๋‹ˆ๊นŒ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ์ ˆ๋ฐ˜ ๋„˜๊ฒŒ ์ค„์—ˆ๋‹ค.

 

(2) ์ฝ”๋“œ by chatGPT

def get_wood_length(trees, height):
    total = 0
    for tree in trees:
        if tree > height:
            total += tree - height
    return total

def find_max_saw_height(trees, target):
    left, right = 0, max(trees)
    result = 0
    
    while left <= right:
        mid = (left + right) // 2
        wood = get_wood_length(trees, mid)
        
        if wood >= target:
            result = mid  # ํ˜„์žฌ ๋†’์ด์—์„œ ๋‚˜๋ฌด๋ฅผ ์ถฉ๋ถ„ํžˆ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฉด, ๋” ๋†’์€ ๋†’์ด๋„ ํ™•์ธ
            left = mid + 1
        else:
            right = mid - 1
    
    return result

# ์ž…๋ ฅ ๋ฐ›๊ธฐ
N, M = map(int, input().split())
trees = list(map(int, input().split()))

# ์ตœ๋Œ€ ์ ˆ๋‹จ๊ธฐ ๋†’์ด ์ถœ๋ ฅ
print(find_max_saw_height(trees, M))

 

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ์ด ์ฝ”๋“œ์™€ ๋‚ด ์ฝ”๋“œ์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๋‚˜๋ฌด๋งˆ๋‹ค ์ž˜๋ผ๋‚ด๋Š” ๊ณผ์ •์˜ ํ•จ์ˆ˜ํ™”์ด๋‹ค. ๋ฐ”๋กœ ์ฑ—์ง€ํ”ผํ‹ฐํ•œํ…Œ ๋‘ ์ฝ”๋“œ์˜ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ์ด๋ ‡๊ฒŒ๋‚˜ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” ์ด์œ  ๋ฌผ์–ด๋ด„

 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ๋ฌธ์ œ ํ’€ ๋•Œ๋Š” ์ข€ ๋œํ•œ๋ฐ, ๋ฐฑ์ค€์—์„œ ๋ฌธ์ œ ํ’€ ๋•Œ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ž˜ ์จ์•ผ๊ฒ ๋‹ค๊ณ  ๋‹ค์งํ•˜๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.