ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2343๋ฒˆ: ๊ธฐํƒ€ ๋ ˆ์Šจ

๊ฐ•ํ† ๋Š” ์ž์‹ ์˜ ๊ธฐํƒ€ ๋ ˆ์Šจ ๋™์˜์ƒ์„ ๋ธ”๋ฃจ๋ ˆ์ด๋กœ ๋งŒ๋“ค์–ด ํŒ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋ธ”๋ฃจ๋ ˆ์ด์—๋Š” ์ด N๊ฐœ์˜ ๋ ˆ์Šจ์ด ๋“ค์–ด๊ฐ€๋Š”๋ฐ, ๋ธ”๋ฃจ๋ ˆ์ด๋ฅผ ๋…นํ™”ํ•  ๋•Œ, ๋ ˆ์Šจ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์•ˆ ๋œ๋‹ค. ์ˆœ์„œ๊ฐ€ ๋’ค๋ฐ”๋€Œ๋Š” ๊ฒฝ๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ธ”๋ฃจ๋ ˆ์ด์— N๊ฐœ์˜ ๋ ˆ์Šจ์ด ๋“ค์–ด๊ฐ€๋Š”๋ฐ, ๋ธ”๋ฃจ๋ ˆ์ด๋ฅผ ๋…นํ™”ํ•  ๋•Œ๋Š” ๋ ˆ์Šจ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์•ˆ ๋˜๊ณ  M๊ฐœ์˜ ๋ธ”๋ฃจ๋ ˆ์ด์— ํ”Œ๋ ˆ์ด ํƒ€์ž„์ด ๋ชจ๋‘ ๊ฐ™๋„๋ก ํ•ด์„œ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ ๊ฐ€๋Šฅํ•œ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ ์ค‘ ์ตœ์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ „ํ˜•์ ์ธ ์ด๋ถ„ ํƒ์ƒ‰ ๋ฌธ์ œ์ด๋‹ค.

 

  • start, end๋ฅผ `max(lesson)`, `sum(lesson)`๋กœ ์„ค์ •ํ•œ๋‹ค.
    • ํ•˜๋‚˜์˜ ๋ ˆ์Šจ์„ ๋‹ด๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ตœ์†Œ lesson์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์€ ๋˜์–ด์•ผ ํ•œ๋‹ค.
    • ํ•˜๋‚˜์˜ ๋ธ”๋ฃจ๋ ˆ์ด์— ๋ชจ๋“  ๋ ˆ์Šจ์„ ๋‹ด๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋ชจ๋“  ๋ ˆ์Šจ ์‹œ๊ฐ„์„ ํ•ฉ์นœ ๊ฒƒ๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด, ๋ธ”๋ฃจ ๋ ˆ์ด์˜ ๋ ˆ์Šจ ์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜๋ฉฐ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.

 

์ฝ”๋“œ

if __name__ == '__main__':
    n, m = map(int, input().split())
    lesson = list(map(int, input().split()))
    start, end = max(lesson), sum(lesson)

    while start <= end:
        mid = (start + end) // 2

        cnt = 0
        play_time = 0
        for l in lesson:
            # play_time์ด m๋ณด๋‹ค ์ปค์ง€๋ฉด ์ƒˆ๋กœ์šด dvd ํ•„์š”
            if play_time + l > mid:
                cnt += 1
                play_time = 0

            play_time += l

        cnt += 1 if play_time else 0

        if cnt <= m:
            end = mid - 1
        else:
            start = mid + 1
    print(start)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€