글 작성자: 망고좋아
반응형

1966, 프린터 큐

📁 문제 출처

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

 

💡 생각

  • 숫자가 높을수록 중요도가 높다.
  • 처음에 출구가 오른쪽인 줄 알고 deque 사용하면서 이것저것 하느라 삽질했다...
  • index가 m인 정수가 몇 번째에 출력되는지 기억해야 되니까 index랑 value를 같이 저장하는 temp(배열)을 만들어주고 시작했다.
  • 그리고 arr배열의 reverse 해줘서 중요도 배열을 정렬해줬다.

 

🛠 나의 코드

t = int(input())

for i in range(t):
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))

    temp = []
    idx = 0

    for i in arr:
        temp.append([idx, i])
        idx += 1

    cnt = 0
    stop = 1
    arr = sorted(arr, reverse= True)

    while stop != 0 :
        for i in range(len(temp)):
            if temp[0][0] == m and temp[0][1] == arr[0]:
                stop = 0
                break
            elif temp[0][1] == arr[0]:
                temp.pop(0)
                arr.pop(0)
                cnt += 1
            else:
                temp.append(temp.pop(0))
    print(cnt+1)
  • 내 코드는 index, value를 저장해주는 배열을 만들었지만, 다른 코드를 보니까 n만큼의 배열을 만들고 m위치에 1로 표시해줘서 함께 비교해 나갔다.

 

개선 코드

t = int(input())

for _ in range(t):
    n, m = map(int,input().split())

    arr = list(map(int,input().split()))
    temp = [0 for _ in range(n)] 
    temp[m] = 1 

    count = 0
    while True:
        if arr[0] == max(arr):
            count += 1

            if temp[0] != 1:
                arr.pop(0)
                temp.pop(0)
            else:
                print(count)
                break
        else:
            arr.append(arr.pop(0))
            temp.append(temp.pop(0))

 

✔️ 배운 점 및 주의할 점

  • 문제를 제대로 읽고 이해하자.
반응형