|
| 1 | +package com.hexeong.baekjoon; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + // AI의 피드백 : Wrapper 클래스 사용을 지양하자. |
| 9 | + // 또한, 코테에서는 클래스의 멤버 메서드 사용하지 말고 배열을 직접 조작하는 방식을 사용하자. |
| 10 | + public static void main(String[] args) throws Exception { |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + int N = Integer.parseInt(st.nextToken()); |
| 15 | + int K = Integer.parseInt(st.nextToken()); |
| 16 | + |
| 17 | + Section[] belt = new Section[2 * N]; |
| 18 | + st = new StringTokenizer(br.readLine()); |
| 19 | + for (int i = 0; i < 2 * N; i++) { |
| 20 | + belt[i] = new Section(false, Integer.parseInt(st.nextToken())); |
| 21 | + } |
| 22 | + |
| 23 | + int round = 0; |
| 24 | + int cntZeroSection = 0; |
| 25 | + int startIdx = 0; |
| 26 | + while (cntZeroSection < K) { |
| 27 | + round++; |
| 28 | + |
| 29 | + // 1. 로봇과 함께 회전 |
| 30 | + startIdx = (startIdx - 1 + 2 * N) % (2 * N); |
| 31 | + int loweridx = (startIdx + N - 1) % (2 * N); |
| 32 | + belt[loweridx].setRobotExists(false); |
| 33 | + |
| 34 | + // 2. N-2부터 0까지 앞으로 1칸 이동 |
| 35 | + int targetIdx = (startIdx + N - 2) % (2 * N); |
| 36 | + int endIdx = startIdx - 1 < 0 ? 2 * N - 1 : startIdx - 1; |
| 37 | + for (int curIdx = targetIdx; curIdx != endIdx; curIdx = (curIdx == 0 ? 2 * N - 1 : curIdx - 1)) { |
| 38 | + int nextIdx = (curIdx + 1) % (2 * N); |
| 39 | + if (belt[curIdx].getRobotExists() |
| 40 | + && !belt[nextIdx].getRobotExists() |
| 41 | + && belt[nextIdx].getDurability() > 0) { |
| 42 | + |
| 43 | + belt[nextIdx].setRobotExists(true); |
| 44 | + belt[nextIdx].setDurability(belt[nextIdx].getDurability() - 1); |
| 45 | + belt[curIdx].setRobotExists(false); |
| 46 | + |
| 47 | + // 2-1. 내구도가 0이 됐으면 cntZeroSection += 1; |
| 48 | + if (belt[nextIdx].getDurability() == 0) |
| 49 | + cntZeroSection++; |
| 50 | + |
| 51 | + if (nextIdx == loweridx) { // N-1 위치에 로봇이 오면 즉시 내린다. |
| 52 | + belt[nextIdx].setRobotExists(false); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // 3. 올리는 위치에 있는 칸의 내구도가 0이 아니라면 올리는 위치에 로봇을 올리기 |
| 58 | + if (belt[startIdx].getDurability() > 0) { |
| 59 | + belt[startIdx].setRobotExists(true); |
| 60 | + belt[startIdx].setDurability(belt[startIdx].getDurability() - 1); |
| 61 | + // 3-1. 내구도가 0이 됐으면 cntZeroSection += 1; |
| 62 | + if (belt[startIdx].getDurability() == 0) |
| 63 | + cntZeroSection++; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + System.out.println(round); |
| 68 | + } |
| 69 | + |
| 70 | + static class Section { |
| 71 | + private Boolean isRobotExists; |
| 72 | + private Integer durability; |
| 73 | + public Section(boolean isRobotExists, Integer durability) { |
| 74 | + this.isRobotExists = isRobotExists; |
| 75 | + this.durability = durability; |
| 76 | + } |
| 77 | + |
| 78 | + public Boolean getRobotExists() { |
| 79 | + return isRobotExists; |
| 80 | + } |
| 81 | + |
| 82 | + public Integer getDurability() { |
| 83 | + return durability; |
| 84 | + } |
| 85 | + |
| 86 | + public void setRobotExists(Boolean robotExists) { |
| 87 | + isRobotExists = robotExists; |
| 88 | + } |
| 89 | + |
| 90 | + public void setDurability(Integer durability) { |
| 91 | + this.durability = durability; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments