[백준] 24511번 queuestack (python)

마뇽미뇽·2025년 9월 8일
0

알고리즘 문제풀이

목록 보기
158/165

1. 문제

https://www.acmicpc.net/problem/24511

2. 풀이

스택이라면 push와 pop한 값이 동일하기 때문에 무시하고 큐라면 enqueue와 dequeue의 값이 다르기 때문에 출력될 것이기 때문에 [1, 4] 를 초기값에서 저장한다.
이후 큐 자료구조인 경우에서만 값들을 모았기 때문에 c의 값을 enqueue(appendleft)해준 후 dequeue(pop) 한 값을 출력한다

3. 코드

import sys
from collections import deque

n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
c = list(map(int, sys.stdin.readline().split()))

qs = deque()
for i in range(n):
    #   큐라면 나가는 수랑 들어오는 수 다름
    if a[i] == 0:
        qs.append(b[i])

    #   스택이라면 나가고 들어오는 수 똑같아서 무시
    continue

for i in range(m):
    #   큐에 대한 조건만 들어있음
    qs.appendleft(c[i])
    print(qs.pop(), end=' ')```
profile
Que sera, sera

0개의 댓글