목차

요세푸스 문제 2

ps
링크acmicpc.net/…
출처BOJ
문제 번호1168
문제명요세푸스 문제 2
레벨플래티넘 4
분류

Order statistic tree

시간복잡도O(nlogn)
인풋사이즈n<=100,000
사용한 언어Python
제출기록42960KB / 568ms
최고기록568ms
해결날짜2021/08/07

풀이

코드

"""Solution code for "BOJ 1168. 요세푸스 문제 2".

- Problem link: https://www.acmicpc.net/problem/1168
- Solution link: http://www.teferi.net/ps/problems/boj/1168
"""


def main():
    def delete_kth(k: int):
        i = 1
        while i < tree_size:
            i += i
            t = tree[i]
            if t < k:
                k -= t
                i += 1
            tree[i] -= 1
        tree[1] -= 1
        return i - tree_size

    N, K = [int(x) for x in input().split()]

    tree_size = 1 << (N - 1).bit_length()
    tree = [0] * (tree_size + tree_size)
    tree[tree_size:tree_size + N] = [1] * N
    for i in range(tree_size - 1, 0, -1):
        tree[i] = tree[i + i] + tree[i + i + 1]
        
    order = 0
    answer = []
    for i in range(N, 0, -1):
        order = (order + K - 1) % i
        num = delete_kth(order + 1)
        answer.append(num + 1)

    print('<', end='')
    print(', '.join(str(x) for x in answer), end='>')


if __name__ == '__main__':
    main()