Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f98937b

Browse files
committed
고다혜: [BOJ] 13334 철로_240121
1 parent e23559f commit f98937b

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

‎BOJ/10001-15000번/DH_13334.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 철로
6+
*/
7+
8+
public class DH_13334 {
9+
static class Node implements Comparable<Node> {
10+
int s, e;
11+
public Node(int s, int e) {
12+
this.s = s; // 시작 지점
13+
this.e = e; // 끝 지점
14+
}
15+
16+
@Override
17+
public int compareTo(Node o) {
18+
return Integer.compare(this.e, o.e); // 끝 지점에 대해 오름차순
19+
}
20+
}
21+
public static void main(String[] args) throws Exception {
22+
23+
// System.setIn(new FileInputStream("./input/BOJ13334.txt"));
24+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
25+
StringTokenizer st;
26+
27+
int n = Integer.parseInt(br.readLine());
28+
PriorityQueue<Node> pq = new PriorityQueue<Node>();
29+
30+
// 입력되는 사무실, 집 지점은 왼쪽 지점을 기준으로 오름차순으로 우선순위를 적용하여 큐에 저장
31+
for(int i = 0; i < n; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
34+
int a = Integer.parseInt(st.nextToken());
35+
int b = Integer.parseInt(st.nextToken());
36+
37+
// 시작점과 끝점이 정렬이 안된 상태에서 주어지기 때문에
38+
// 입력 값에서 최소, 최대를 구해서 시작, 끝점을 구함
39+
int s = Math.min(a, b), e = Math.max(a, b);
40+
41+
pq.add(new Node(s, e));
42+
}
43+
44+
int d = Integer.parseInt(br.readLine());
45+
int startLimit = -100_000_000; // 처음 왼쪽 기준점을 최소값으로 설정
46+
int result = 0;
47+
48+
// 입력을 받을 때는 끝점에 대해 오름차순이었지만
49+
// 철도 범위 안의 사람의 수를 구하기 위해서는 시작점 기준으로 오름차순으로 하는 우선순위큐가 추가로 있어야 됨
50+
// 시작점 오름차순으로 해야되는 이유: 왼쪽 시작점을 크기가 작은거부터 큰거 순서로 확인해야 되기 때문
51+
PriorityQueue<Node> tmp = new PriorityQueue<Node>((o1, o2) -> Integer.compare(o1.s, o2.s));
52+
53+
while(!pq.isEmpty()) {
54+
Node current = pq.poll();
55+
56+
// 집과 사무실 사이의 거리가 철도의 길이보다 길다면 넘어가기
57+
if(current.e - current.s > d) continue;
58+
59+
// 현재 사람의 오른쪽 지점 - 왼쪽 기준점이 철도의 길이보다 길다면
60+
if(current.e - startLimit > d) {
61+
62+
tmp.add(current); // tmp큐에 넣어주기
63+
64+
// current를 tmp에 넣게 되면서 왼쪽 시작점의 위치를 조정해야될 수도 있음
65+
while(!tmp.isEmpty()) {
66+
// 현재 끝 지점 - 가능한 왼쪽 시작점 <= d가 될 때까지 tmp.poll 하기
67+
if(current.e - tmp.peek().s > d) {
68+
tmp.poll();
69+
} else break;
70+
}
71+
// 왼쪽 시작점 조정해주기
72+
startLimit = Math.min(current.s, tmp.peek().s);
73+
74+
}
75+
// 현재 사람의 오른쪽 기점 - 왼쪽 기준점 <= 철도의 길이
76+
else {
77+
// tmp큐에 넣어주기
78+
tmp.add(current);
79+
80+
// current에서 시작점이 현재 왼쪽 기준점보다 작아질 수 있기 때문에
81+
// startLimit 값 조정해주기
82+
startLimit = Math.min(startLimit, current.s);
83+
}
84+
85+
result = Math.max(result, tmp.size());
86+
}
87+
88+
System.out.println(result);
89+
}
90+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /