240604 컨베이어 벨트 위의 로봇

Jongleee·2024년 6월 4일
0

TIL

목록 보기
590/737
public static void main(String[] args) throws Exception {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String[] input = br.readLine().split(" ");

	int n = Integer.parseInt(input[0]);
	int k = Integer.parseInt(input[1]);

	int beltLength = n * 2;
	boolean[] hasRobot = new boolean[beltLength];
	int[] durability = new int[beltLength];

	input = br.readLine().split(" ");
	for (int i = 0; i < beltLength; i++) {
		durability[i] = Integer.parseInt(input[i]);
	}

	int steps = 0;
	int start = 0;
	int end = n - 1;
	int lastIndex = beltLength - 1;
	int current;
	int next;

	while (k > 0) {
		start = (start - 1 < 0) ? lastIndex : start - 1;
		end = (end - 1 < 0) ? lastIndex : end - 1;

		current = end;
		hasRobot[end] = false;
		while (true) {
			next = current;
			current = (current - 1 < 0) ? lastIndex : current - 1;

			if (hasRobot[current] && !hasRobot[next] && durability[next] > 0) {
				hasRobot[current] = false;
				if (end != next)
					hasRobot[next] = true;

				if (--durability[next] == 0)
					k--;
			}

			if (current == start)
				break;
		}

		if (durability[start]-- > 0) {
			if (durability[start] == 0)
				k--;

			hasRobot[start] = true;
		}
		steps++;
	}

	System.out.println(steps);
}

출처:https://www.acmicpc.net/problem/20055

0개의 댓글