[백준] 2346번: 풍선 터뜨리기 (MUST RETRY!!!!!!!!!!!!!!!!!)

whitehousechef·2023년 10월 29일
0

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

initial

i tried solving it brute-force way but i couldnt

you can do it like this but using deque.rotate() is much easier

n=int(input())
s=list(map(int,input().split()))
start=0
index=[x for x in range(1,n+1)]
answer=[]
temp = s.pop(start)
answer.append(index.pop(start))

while s:
    if temp<0:
        start = (start+temp)%len(s)
    else:
        start=(start+temp-1)%len(s)
    temp = s.pop(start)
    answer.append(index.pop(start))
for i in answer:
    print(i,end=' ')

solution

https://velog.io/@hygge/Python-%EB%B0%B1%EC%A4%80-2346-%ED%92%8D%EC%84%A0-%ED%84%B0%EB%9C%A8%EB%A6%AC%EA%B8%B0-deque

a visualisation of how deque.rotate() works. If rotate(-value), we turn it anticlockwise by value times. If it is positive number we turn it clockwise.

we ennumerate from index 1 via giving it the parameter of start=1. If we have a positive val, that means we need to get the valth index of that deque. We can get it by moving the front elements in front of that desired index to the back of the deque via going anti-clockwise. We have to do -(val-1) and is -1 cuz we want to move 1 step less cuz we already popped one value so our length of deque is 1 length shorter than original.

if val is negative, we can either turn clockwise or anti but if we are matching it to anti clockwise, (tbc i dont get it )

import sys
input = sys.stdin.readline
from collections import defaultdict, deque
import math

n=int(input())
queue = deque(enumerate(map(int,input().split()), start=1))
ans=[]
while queue:
    hola= queue.popleft()
    ans.append(hola[0])
    val = hola[1]
    if val>0:
        queue.rotate(-(val-1))
    else:
        queue.rotate(-val)
print(*ans)

0개의 댓글