250211 철로

Jongleee·2025년 2월 11일
0

TIL

목록 보기
806/858
private static class Line {
	long start, end;

	public Line(long start, long end) {
		this.start = Math.min(start, end);
		this.end = Math.max(start, end);
	}
}

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int n = Integer.parseInt(br.readLine().trim());
	List<Line> lines = new ArrayList<>();

	for (int i = 0; i < n; i++) {
		StringTokenizer st = new StringTokenizer(br.readLine().trim());
		lines.add(new Line(Long.parseLong(st.nextToken()), Long.parseLong(st.nextToken())));
	}

	long d = Long.parseLong(br.readLine().trim());
	System.out.println(findMaxLines(lines, d));
}

private static int findMaxLines(List<Line> lines, long d) {
	List<Long> startPoints = new ArrayList<>();
	List<Long> endPoints = new ArrayList<>();

	for (Line line : lines) {
		if (line.start + d >= line.end) {
			startPoints.add(line.end - d);
			endPoints.add(line.start);
		}
	}

	if (startPoints.isEmpty())
		return 0;

	Collections.sort(startPoints);
	Collections.sort(endPoints);

	int maxCount = 0, count = 0;
	int i = 0, j = 0;

	while (i < startPoints.size()) {
		if (startPoints.get(i) > endPoints.get(j)) {
			count--;
			j++;
		} else {
			count++;
			maxCount = Math.max(maxCount, count);
			i++;
		}
	}
	return maxCount;
}

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

0개의 댓글

관련 채용 정보