알고리즘 문제풀이

Swea D3_11885.피자굽기 Python Queue

아크몽 2024. 2. 16. 16:15

정석적인 Queue 사용

T = int(input())
for tc in range(1,T+1):
    N,M = map(int,input().split())
    arr = list(map(int,input().split()))
    arr_ = [(i,arr[i]) for i in range(M)]
    # print(arr)
    oven = arr_[:N]
    pre_oven = arr_[N:M]
    while len(oven) > 1:
        idx, cheese = oven.pop(0)
        cheese //= 2
        if cheese != 0:
            oven.append([idx,cheese])
        elif cheese == 0 and pre_oven:
            oven.append(pre_oven.pop(0))
    print(f'#{tc} {oven[0][0] + 1}')

 

그리디하게 풀려고 했던 코드 (4pass? 였나)

#N개의 피자
# 1~M+1
T = int(input())
for tc in range(1,T+1):
    N,M = map(int,input().split())
    arr = list(map(int,input().split()))
    num_arr = []
    for i in range(M):
        num_arr.append([arr[i],i])
    oven = []
    for i in range(N):
        oven.append(num_arr[i])
    # print(oven)
    rear = -1
    cnt = 0
    while True:
        rear += 1
        oven[rear%N][0] = oven[rear%N][0] // 2
        if oven[rear % N][0] == 0:
            oven.pop(rear % N) # 오븐의 인덱스를 pop
            if N+cnt < M:
                oven.insert(rear % N,num_arr[N+cnt]) # 오븐의 인덱스에 insert
                cnt += 1
            else: #뽑을게 없다
                oven.append([-1,-1])
        if oven.count([-1,-1]) == N-1:
            break
    print(f'#{tc} {oven[0][1]+1}')