Skip to content

Conversation

@eddie-liner
Copy link
Collaborator

@eddie-liner eddie-liner commented Nov 3, 2022

Signed-off-by: Kimyungi gozj3319@agape.hanyang.ac.kr

Coding Test Study Pull Request


문제 정보

문제 링크

문제 관련 알고리즘 or 자료구조

  • 큐, 시뮬레이션

해결 과정

작성 코드

import sys
from queue import deque
input = sys.stdin.readline

if __name__ == '__main__':
    m, t, n = map(int, input().strip().split())
    time = 0
    curr = 'left'
    left_q = deque()
    right_q = deque()
    for i in range(n):
        arrive_time, location = input().strip().split()
        if location == 'left':
            left_q.append((int(arrive_time), i))
        else:
            right_q.append((int(arrive_time), i))
        
    answer = [0] * (len(left_q) + len(right_q))
    while left_q or right_q:
        cnt = 0
        if curr == 'left':
            if left_q:
                left = left_q[0][0]
            else:
                left = 100001
            if right_q:
                right = right_q[0][0]
            else:
                right = 100001
                
            if left <= time or left <= right: # left start
                time = max(time, left)
                while left_q and left_q[0][0] <= time and cnt < m:
                    _, idx = left_q.popleft()
                    cnt += 1
                    answer[idx] = time + t
                time += t
                curr = 'right'
            else: # right start
                time = max(time, right) + t
                while right_q and right_q[0][0] <= time and cnt < m:
                    _, idx = right_q.popleft()
                    cnt += 1
                    answer[idx] = time + t
                time += t
        else:
            if left_q:
                left = left_q[0][0]
            else:
                left = 100001
            if right_q:
                right = right_q[0][0]
            else:
                right = 100001
                
            if right <= time or right <= left: # right start
                time = max(time, right)
                while right_q and right_q[0][0] <= time and cnt < m:
                    _, idx = right_q.popleft()
                    cnt += 1
                    answer[idx] = time + t
                time += t
                curr = 'left'
            else: # left start
                time = max(time, left) + t
                while left_q and left_q[0][0] <= time and cnt < m:
                    _, idx = left_q.popleft()
                    cnt += 1
                    answer[idx] = time + t
                time += t
            
    for i in answer:
        print(i)

어려웠던 점

  • 큐를 두 개 사용해야 한다는 점과 입력받은 순서대로 출력해야 한다는 점을 모두 고려하는 것이 어려웠어여..

궁금한 점

  • 없음

기타

  • 값을 비교해서 확인해야 할 땐, popleft을 쓰는 것보다 indexing을 통해 값을 할당하는 것이 자잘한 에러 없이 코드를 짤 수 있네요..!

Signed-off-by: Kimyungi <gozj3319@agape.hanyang.ac.kr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

백준 백준

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant