(백준) 제11866호. 조셉 질문 0 -파이썬

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

#11866: 조셉 문제 0

첫 번째 줄에는 N과 K가 공백을 두고 순차적으로 입력됩니다.

(1≤K≤N≤1,000)

www.acmicpc.net

첫 번째 방법 – 실패

s = 2
idx = 0
del_idx = s+idx
cnt =0
while True:
    if not li:
        print(ans_li)
        break
    tmp=li.pop(s)
    s += 2
    ans_li.append(tmp)
    print(li,s)
    if len(li) < s:
        s -= 5 # s = -3

솔루션 – 양방향 대기열 사용

조셉 시퀀스

ex) 1234567이면 세 번째는 12를 거꾸로 보낸다.

이를 이용하여 k-1, 즉 인덱스 0, 1st를 뒤로 보내고 popleft에서 이전 3을 뺍니다.

456712에서 세 번째를 반복적으로 제거하려면 45를 추가하고 popleft() 6을 뒤로 추가합니다.

import sys
from collections import deque
input = sys.stdin.readline
n,k=map(int,input().split())

queue = deque()
for i in range(1,n+1):
    queue.append(i)
ans_li=()
print('<',end='')
while queue:
    for i in range(k-1):
        queue.append(queue.popleft())
    print(f'{queue.popleft()}',end='')
    if queue:
        print(end=', ')
print('>')
error: Alert: Content selection is disabled!!