알고리즘
자료구조
[13335] 트럭
by Hwang
문제
https://www.acmicpc.net/problem/13335
풀이
큐를 이용한 풀이입니다.
먼저 다리를 표현할 큐를 만들어줍니다. 다리의 길이와 동일하게 w 크기를 가진 큐를 만들어 주는데 아직 다리에 아무 트럭이 존재하지 않기 때문에 모든 데이터는 0입니다.
큐를 만들었다면 모든 트럭이 건너갈 때까지 시뮬레이션을 해주면 됩니다.
시간이 지날 때마다 큐의 왼쪽 값을 제거해주고, 건널 수 있는 트럭이 있다면 큐에 넣어주거나 없을 경우 0을 넣어주면 됩니다.
import sys
from collections import deque
N, W, L = map(int, sys.stdin.readline().split())
trucks = tuple(map(int, sys.stdin.readline().strip().split()))
bridge = deque([0] * W)
total_weight, total_time = 0, 0
index = 0
while bridge:
total_time += 1
total_weight -= bridge.popleft()
if index < N:
if total_weight + trucks[index] <= L:
bridge.append(trucks[index])
total_weight += trucks[index]
index += 1
else:
bridge.append(0)
print(total_time)
Comment