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()