• simulation
  • random access를 해야하기 때문에 queue 말고 list를 사용했다.
  • 0 ~ N-1을 해당 순서로 담는 overList, N ~ 2N-1을 해당 순서로 담는 underList를 정의했다.
  • 내구도와 로봇 존재 여부의 flag 값을 갖는 Node를 정의했다.
  • 진행 방식과 동일하게 메서드를 크게 moveBelt, moveRobot, liftRobot로 나누었다.
  • calZeroCount 메서드로 한 cycle이 새로 시작하기 전에 현재 내구도가 0인 벨트의 개수를 계산한다.
  • 로봇을 한 칸 이동 시킬 때, N 위치에 있는 로봇은 항상 컨베이어 벨트에서 내려준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

	static class Node {
		int durability;
		boolean robotOn;

		Node(int durability, boolean robotOn) {
			this.durability = durability;
			this.robotOn = robotOn;
		}
	}

	static int N, K;
	static List<Node> overList = new ArrayList<>();
	static List<Node> underList = new ArrayList<>();

	static int calZeroCount() {
		int cnt = 0;
		for (Node e : overList)
			if (e.durability == 0) cnt++;
		for (Node e : underList)
			if (e.durability == 0) cnt++;

		return cnt;
	}

	static void moveBelt() {
		Node last = overList.remove(overList.size() - 1);
		last.robotOn = false;
		underList.add(0, last);

		last = underList.remove(underList.size() - 1);
		overList.add(0, last);
	}

	static void moveRobot() {
		overList.get(N - 1).robotOn = false;

		for (int i = N - 1; i > 0; i--) {
			if \(overList.get\(i).robotOn == true \|| overList.get\(i - 1).robotOn == false)
				continue;
			if (overList.get(i).durability <= 0)
				continue;

			overList.get(i).robotOn = true;
			overList.get(i - 1).robotOn = false;
			overList.get(i).durability--;
		}
	}

	static void liftRobot() {
		Node first = overList.get(0);
		if (first.durability > 0) {
			first.durability--;
			first.robotOn = true;
		}
	}

	static int solution() {
		int level = 0;
		while (true) {
			if (calZeroCount() >= K) break;

			moveBelt();
			moveRobot();
			liftRobot();
			level++;
		}
		return level;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		String[] firstLine = in.readLine().split(" ");
		N = Integer.parseInt(firstLine[0]);
		K = Integer.parseInt(firstLine[1]);
		StringTokenizer st = new StringTokenizer(in.readLine(), " ");
		for (int i = 0; i < N; i++)
			overList.add(new Node(Integer.parseInt(st.nextToken()), false));
		for (int i = 0; i < N; i++)
			underList.add(new Node(Integer.parseInt(st.nextToken()), false));

		System.out.println(solution());
	}
}