๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 20055๋ฒ
ํ์ด ๊ณผ์
๋ฌธ์ ์์ ์ ์ํ ๊ท์น๋๋ก ๋์ํ๋ ์ปจ๋ฒ ์ด์ด ๋ฒจํธ๋ฅผ ๊ตฌํํ๋ฉด ๋๋ ๋ฌธ์ ์
๋๋ค.
2์ฐจ์ ๋ฐฐ์ด์ ์ด์ฉํด์ ์ปจ๋ฒ ์ด์ด ๋ฒจํธ๋ฅผ ๋ํ๋์ง๋ง ์ฌ์ค ๊ทธ๋ฅ 1์ฐจ์ ๋ฐฐ์ด์ ์ด์ฉํด์ ๊ตฌํํด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฌธ์ ์์ ์ฃผ์ด์ง ๋ค ๊ฐ์ง ๋จ๊ณ๋ฅผ ๊ฐ๊ฐ์ ํจ์๋ก ๊ตฌํํด์ ๊ฒ์ฌํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ต๋๋ค.
์ฝ๋
import sys
from collections import deque
N, K = list(map(int, sys.stdin.readline().split()))
belts = list(map(int, sys.stdin.readline().split()))
def init_conveyor():
conveyor = [[0] * N for _ in range(2)]
conveyor[0] = [*belts[:N]]
conveyor[1] = [*belts[N:][::-1]]
return conveyor
def rotate_belt(conveyor, robots):
# ์ปจ๋ฒ ์ด์ด ๋ฒจํธ ์ด๋
cache1 = conveyor[0][N - 1]
for y in range(N - 1, 0, -1):
conveyor[0][y] = conveyor[0][y - 1]
cache2 = conveyor[1][0]
for y in range(N - 1):
conveyor[1][y] = conveyor[1][y + 1]
conveyor[0][0] = cache2
conveyor[1][N - 1] = cache1
# ๋ก๋ด ์ด๋
robots.pop()
robots.appendleft(0)
def move_robots(conveyor, robots):
for idx in range(N - 1, -1, -1):
if robots[idx]:
next_idx = idx + 1
if robots[next_idx] == 0 and conveyor[0][next_idx] > 0:
conveyor[0][next_idx] -= 1
robots[idx] = 0
robots[next_idx] = 1
def add_robot(conveyor, robots):
if conveyor[0][0] > 0 and robots[0] != 1:
robots[0] = 1
conveyor[0][0] -= 1
def check_belt(conveyor):
cnt = 0
for x in range(2):
for y in range(N):
if not conveyor[x][y]:
cnt += 1
return cnt < K
def check_endpoint(robots):
if robots[N - 1]:
robots[N - 1] = 0
def solution():
answer = 1
conveyor = init_conveyor()
robots = deque([0] * N)
while True:
rotate_belt(conveyor, robots)
check_endpoint(robots)
move_robots(conveyor, robots)
check_endpoint(robots)
add_robot(conveyor, robots)
go = check_belt(conveyor)
if not go:
break
answer += 1
return answer
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 9625 - BABBA (0) | 2021.03.08 |
---|---|
BOJ 11952 - ์ข๋น (0) | 2021.03.08 |
BOJ 20044 - Project Teams (0) | 2021.03.08 |
BOJ 1644 - ์์์ ์ฐ์ํฉ (0) | 2021.03.08 |
BOJ 18242 - ๋ค๋ชจ๋ค๋ชจ ์๋ ฅ๊ฒ์ฌ (0) | 2021.03.08 |
๐ฌ ๋๊ธ