Skip to content

Commit 08289fb

Browse files
committed
[BOJ] #2828. 사과 담기 게임 / 실버5 / 35분 / 성공
1 parent 4b3eae4 commit 08289fb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, M = map(int, input().split()) # N: 스크린 칸 수, M: 바구니 크기
5+
J = int(input()) # 떨어지는 사과의 개수
6+
apple_pos = [] # 떨어지는 사과의 위치
7+
for _ in range(J):
8+
apple_pos.append(int(input()))
9+
10+
# 바구니 초기 위치: 스크린 가장 왼쪽
11+
basket_left = 1
12+
basket_right = M
13+
distance = 0 # 이동거리
14+
15+
for apple in apple_pos:
16+
# 바구니가 사과를 담을 수 있는 위치에 있는 경우
17+
if basket_left <= apple <= basket_right:
18+
continue
19+
# 바구니가 사과의 왼쪽에 있는 경우
20+
elif basket_left < apple:
21+
distance += apple - basket_right
22+
basket_right = apple
23+
basket_left = apple - M + 1
24+
# 바구니가 사과의 오른쪽에 있는 경우
25+
elif apple < basket_right:
26+
distance += basket_left - apple
27+
basket_left = apple
28+
basket_right = apple + M - 1
29+
30+
print(distance)

0 commit comments

Comments
 (0)