Skip to content
CatBus

Tag: queue

All the articles with the tag "queue".

BOJ13335SILVER 1
n, w, L = map(int, input().split())
trucks = list(map(int, input().split()))

bridge = deque([0] * w)
t = 0
cur_w = 0
i = 0

while i < n:
    t += 1
    cur_w -= bridge.popleft()
    if cur_w + trucks[i] <= L:
        bridge.append(trucks[i])
        cur_w += trucks[i]
        i += 1
    else:
        bridge.append(0)

t += w
print(t)

트럭

백준 13335번 '트럭' (실버 1) 문제 풀이. implementation, data structures, simulation 로 접근했다.

2025.09.28·1분·implementation
BOJ1966SILVER 3
import heapq

def nag_int(s):
    """음수 정수를 반환"""
    return -int(s)

TC = int(input())
for t in range(TC):
    N, M = map(int, input().split())
    importance_list = list(map(nag_int, input().split()))
    q = deque([(i, im) for i, im in enumerate(importance_list)])
    heapq.heapify(importance_list)

    cnt = 1
    cur_min = heapq.heappop(importance_list)

    while q:
        i, im = q.popleft()
        if i == M and cur_min == im:
            # M 번째 출력되면 끝
            break
        elif cur_min == im:
            # 출력 가능하면 다음으로
            cur_min = heapq.heappop(importance_list)
            cnt += 1
        else:
            # 출력 안되면 대기열 맨 뒤로
            q.append((i, im))
    print(cnt)

프린터 큐

백준 1966번 '프린터 큐' (실버 3) 문제 풀이. implementation, data structures, simulation 로 접근했다.

2025.06.26·3분·implementation
BOJ10845SILVER 4
  • push X: deque.append(X)로 큐의 뒤에 원소를 추가
  • pop: deque.popleft()로 큐의 앞에서 원소를 제거하고 반환
  • size: len(deque)로 큐의 크기 반환
  • empty: 큐가 비어있는지 확인
  • front: deque[0]로 큐의 첫 번째 원소 접근
  • back: deque[-1]로 큐의 마지막 원소 접근

모든 연산에서 큐가 비어있을 때의 예외 처리를 해주어야 한다.

from collections import deque
import sys

N = int(sys.stdin.readline().strip())

q = deque()

for i in range(N):
  cmd = sys.stdin.readline().strip().split()
  if cmd[0] == 'push':
    q.append(cmd[1])

  elif cmd[0] == 'pop':
    if len(q) != 0:
      print(q.popleft())
    else:
      print(-1)

  elif cmd[0] == 'size':
    print(len(q))

  elif cmd[0] == 'empty':
    if len(q) == 0:
      print(1)
    else:
      print(0)

  elif cmd[0] == 'front':
    if len(q) != 0:
      print(q[0])
    else:
      print(-1)
      
  elif cmd[0] == 'back':
    if len(q) != 0:
      print(q[-1])
    else:
      print(-1)

백준 10845번 '큐' (실버 4) 문제 풀이. data structures, queue 로 접근했다.

2025.03.08·5분·data structures