240430 어두운 굴다리

Jongleee·2024년 4월 30일
0

TIL

목록 보기
560/576
public static void main(String[] args) throws IOException {
	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	int length = Integer.parseInt(reader.readLine());
	int lamps = Integer.parseInt(reader.readLine());
	
	int[] positions = new int[length + 1];
	StringTokenizer tokenizer = new StringTokenizer(reader.readLine(), " ");
	int firstPosition = Integer.parseInt(tokenizer.nextToken());
	positions[firstPosition]++;
	int lastPosition = firstPosition;
	for (int i = 0; i < lamps - 1; i++) {
		lastPosition = Integer.parseInt(tokenizer.nextToken());
		positions[lastPosition]++;
	}

	int maxHeight = 1;
	for (int i = firstPosition; i < lastPosition;) {
		int nextPosition = i + 1;
		while (positions[nextPosition] == 0) {
			nextPosition++;
		}
		int space = (nextPosition - i + 1) / 2;
		if (space > maxHeight) maxHeight = space;
		i = nextPosition;
	}
	if (firstPosition > maxHeight) maxHeight = firstPosition;
	if (length - lastPosition > maxHeight) maxHeight = length - lastPosition;

	System.out.print(maxHeight);
}

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

0개의 댓글