Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOJ 1021. 회전하는 큐 #103

Open
hojongs opened this issue Jun 19, 2022 · 0 comments
Open

BOJ 1021. 회전하는 큐 #103

hojongs opened this issue Jun 19, 2022 · 0 comments

Comments

@hojongs
Copy link
Owner

hojongs commented Jun 19, 2022

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

Impl plan

in:
3 3
1 2 3
out:
0
explain:
1,2,3
2,3
3

in:
3 3
2 3 1
out:
1
explain:
1,2,3
2,3,1*
3,1
1

in:
10 3
2 9 5
out:
8
explain:
1,2,3,4,5,6,7,8,9,10
2,3,4,5,6,7,8,9,10,1 (1)
3,4,5,6,7,8,9,10,1 (3 or 6, len=9)
9,10,1,3,4,5,6,7,8 (3 or 6, len=9)
10,1,3,4,5,6,7,8
5,6,7,8,10,1,3,4 (4, len=8)
6,7,8,10,1,3,4

Impl

from collections import deque
import sys
readl = sys.stdin.readline

n, m = [int(i) for i in readl().split()]
nums = [int(i) for i in readl().split()]
que = deque(range(1, n+1))
cnt_total = 0
for num in nums:
    cnt = 0
    while que[0] != num:
        que.append(que.popleft())
        cnt += 1
    cnt_total += min(cnt, len(que)-cnt)
    que.popleft()
print(cnt_total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant