๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 13335 - ํŠธ๋Ÿญ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 13335๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋‹ค๋ฆฌ์— ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ์„ ๋•Œ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ์„œ ๋‹ค๋ฅธ ์ง€์—ญ์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

ํ˜„์žฌ ๋‹ค๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ์ถ”์ฒ™ํ•˜๊ณ  ํŠธ๋Ÿญ์˜ ์‹œ๊ฐ„์„ ๋ณ„๋„์˜ ๋ฐฐ์—ด๋กœ ๊ด€๋ฆฌํ•ด์„œ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ next ๋ณ€์ˆ˜๋Š” ํ˜„์žฌ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๊ธฐ ์œ„ํ•ด ๋Œ€๊ธฐ์ค‘์ธ ํŠธ๋Ÿญ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

N, W, L = list(map(int, sys.stdin.readline().split()))
weights = list(map(int, sys.stdin.readline().split()))


def solution():
    goal, next = W + 1, 1
    on_bridge = deque([[1, weights[0]]])
    on_weight = weights[0]
    answer = 1

    while on_bridge:
        answer += 1

        for truck in on_bridge:
            truck[0] += 1

        if on_bridge[0][0] == goal:
            on_weight -= on_bridge[0][1]
            on_bridge.popleft()

        if next < N and weights[next] + on_weight <= L:
            on_bridge.append([1, weights[next]])
            on_weight += weights[next]
            next += 1

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 16472 - ๊ณ ๋ƒฅ์ด  (0) 2021.03.14
BOJ 17086 - ์•„๊ธฐ ์ƒ์–ด2  (0) 2021.03.08
BOJ 6497 - ์ „๋ ฅ๋‚œ  (0) 2021.03.08
BOJ 6156 - Cow Contest  (0) 2021.03.08
BOJ 9625 - BABBA  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€