20055-g5 성공

This commit is contained in:
2026-03-11 10:40:37 +09:00
parent 93ea0d98a9
commit 7c30191f63

View File

@@ -0,0 +1,75 @@
# 컨베이어 벨트 위의 로봇
import sys
from collections import deque
input = sys.stdin.readline
def solution():
n, k = map(int, input().rstrip().split())
health = [-1] + list(map(int, input().rstrip().split()))
robot = [0] * (2*n+1)
robot_order = deque([])
front = deque([i+1 for i in range(n)])
back = deque([i for i in range(2*n, n, -1)])
unhealth_count = 0
result = 0
while unhealth_count < k:
result += 1
front.appendleft(back.popleft())
back.append(front.pop())
if robot[front[-1]] == 1:
robot_order.popleft()
robot[front[-1]] = 0
now_robot_len = len(robot_order)
for _ in range(now_robot_len):
idx = robot_order.popleft()
nxt = idx + 1
if idx == (2*n):
nxt = 1
if robot[nxt] == 0 and health[nxt] > 0:
robot[idx] = 0
robot[nxt] = 1
robot_order.append(nxt)
health[nxt] -= 1
if health[nxt] == 0:
unhealth_count += 1
else:
robot_order.append(idx)
if robot[front[-1]] == 1:
robot_order.popleft()
robot[front[-1]] = 0
if robot[front[0]] == 0 and health[front[0]] > 0:
robot_order.append(front[0])
robot[front[0]] = 1
health[front[0]] -= 1
if health[front[0]] == 0:
unhealth_count += 1
print(result)
return
solution()
"""
걸린 시간: 26분
시간 복잡도: 내구도가 1000까지이고, 이게 다 깎인 것이 k개가 될때까지이므로 O(1000*k)인데, 중간에 로봇이 한칸씩 움직이기 때문에
컨베이어벨트 윗부분인 n만큼 for문을 돈다. 근데 나는 로봇이 있는 칸만 확인하기 때문에 O(1000*k*n)보다는 적다.
해설: 문제조건대로 그대로 구현하면 된다. 벨트가 도는 것은 2개의 deque를 활용해서 구현하였고, 로봇이 있는 칸만 확인하기 위해 robot이 들어간 순서를
기록해놓는 robot_order를 queue로 구현한다. 각 단계를 구현하는 것은 기록할 것들만 잘 생각해서 빼먹지 않으면 어렵지 않게 가능하다.
또한 2n에서 다음 칸이 1이기 때문에 이것만 예외처리 하면 된다.
로봇을 올릴 경우 로봇을 내릴 경우
robot: 로봇 유무 | 1 | 0
health: 내구도 | -1 | -
robot_order: 로봇 올린 순서 | append(nxt) | popleft(now)
"""