Algorithm Problems

[백준][Python] 20301번 반전 요세푸스 - 원형 큐

WakaraNai 2021. 8. 4. 16:43
728x90
반응형

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

 

20301번: 반전 요세푸스

첫째 줄에 정수 $N$, $K$, $M$이 주어진다. ($1 \leq N \leq 5\ 000$, $1 \leq K, M \leq N$)

www.acmicpc.net

 

 

Python

n, k, m = map(int, input().split())
arr = [i for i in range(1, n+1)]
front, rear = 0, n-1
kstep = 0
mstep = 0
go = 1
while n:
    # for i in range(front, front+n):
    #    print(arr[i % len(arr)], end=" ")
    # print()
    kstep += 1

    if kstep % k == 0:
        if go:
            print(arr[front])
            front = (front+1) % len(arr)
        else:
            print(arr[rear])
            rear = (rear-1) % len(arr)

        n -= 1
        mstep += 1
        if mstep % m == 0:
            go = 1 - go

    else:
        if go:
            rear = (rear+1) % len(arr)
            arr[rear] = arr[front]
            front = (front+1) % len(arr)
        else:
            front = (front-1) % len(arr)
            arr[front] = arr[rear]
            rear = (rear-1) % len(arr)
728x90
반응형