미소를뿌리는감자의 코딩

[백준 2024/02/14] 5957번 Cleaning the Dishes 본문

코딩 테스트/백준

[백준 2024/02/14] 5957번 Cleaning the Dishes

미뿌감 2024. 2. 14. 15:47
728x90

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

 

5957번: Cleaning the Dishes

Bessie and Canmuu are teaming up to wash the massive pile of N (1 <= N <= 10,000) dirty dishes left over after the CowMoose Festival. Bessie is washing the dishes; Canmuu will dry them. Each dish has a unique serial number in the range 1..N. At the beginni

www.acmicpc.net

1. 접근 방법

3가지 단계가 있다.

1. 접시 씻기 전 단계

2. 씻은 후 단계 : [ 1 3 의 뜻은 씻은 후 단계(1) 를 3개 진행하라는 뜻이다]
3. 말린 후 단계 : [ 2 2 의 뜻은 말린 후 단계(2) 를 2개 진행하리는 뜻이다]

 

또한 접시들을 n개에 대해서 다음과 같이 쌓여 있다.

1

2

3

4

5

______________

여기서 1 3 이 들어왔다.

          3

4        2

5        1

______________

 

이렇게 다음 단계로 3개의 접시를 넘긴다. 위에서 하나씩 꺼내와서 쌓는다.

 

2 2 가 들어오게 된다면 어떻게 될까. 씻은 후 단계에 있는 접시 중 2개를 위에서 부터 가지고 와서 말린 후 단계로 넘긴다.

 

4             2

5      1     3

_______________

 

이를 구현하기 위해서 stack 을 이용하고자 하였다.

하지만 python에서는 stack 이 구현되어 있지 않다. 따라서 list를 stack 처럼 사용하였다.

 

mid.append(start.pop()) # 접시 씻는 단계
end.append(mid.pop()) # 접시 건조시키는 단계

 

2. 코드

plate = int(input())

start = [i for i in range(plate, 0, -1)]
mid = []
end = []

while len(start) != 0  or len(mid) != 0:
    op, num = map(int, input().split())
    if op == 1:
        for i in range(num):
            mid.append(start.pop())

    else:
        for i in range(num):
            end.append(mid.pop())


for i in range(plate-1, -1, -1):
    print(end[i])
728x90