|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.PriorityQueue; |
| 5 | +import java.util.Queue; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class BJ_13334_철로 { |
| 9 | + |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int n; |
| 15 | + private static int d; |
| 16 | + private static int ans; |
| 17 | + |
| 18 | + private static Node[] nodes; |
| 19 | + private static Queue<Integer> q; |
| 20 | + |
| 21 | + private static class Node implements Comparable<Node> { |
| 22 | + int h; |
| 23 | + int o; |
| 24 | + |
| 25 | + Node(int h, int o) { |
| 26 | + this.h = h; |
| 27 | + this.o = o; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public int compareTo(Node o) { |
| 32 | + if (this.o == o.o) { |
| 33 | + return Integer.compare(this.h, o.h); |
| 34 | + } |
| 35 | + return Integer.compare(this.o, o.o); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String toString() { |
| 40 | + return "[h=" + h + ", o=" + o + "]"; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args) throws IOException { |
| 45 | + init(); |
| 46 | + sol(); |
| 47 | + } |
| 48 | + |
| 49 | + private static void init() throws IOException { |
| 50 | + n = Integer.parseInt(br.readLine()); |
| 51 | + q = new PriorityQueue<>(); |
| 52 | + ans = 0; |
| 53 | + nodes = new Node[n]; |
| 54 | + |
| 55 | + for (int i = 0; i < n; i++) { |
| 56 | + st = new StringTokenizer(br.readLine()); |
| 57 | + int h = Integer.parseInt(st.nextToken()); |
| 58 | + int o = Integer.parseInt(st.nextToken()); |
| 59 | + |
| 60 | + if (h > o) { |
| 61 | + nodes[i] = new Node(o, h); |
| 62 | + } else { |
| 63 | + nodes[i] = new Node(h, o); |
| 64 | + } |
| 65 | + } |
| 66 | + d = Integer.parseInt(br.readLine()); |
| 67 | + Arrays.sort(nodes); |
| 68 | + } |
| 69 | + |
| 70 | + private static void sol() throws IOException { |
| 71 | + for (int i = 0; i < n; i++) { |
| 72 | + // 집-회사 거리가 d보다 큼 |
| 73 | + if (nodes[i].o - nodes[i].h > d) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // 시작점 저장 |
| 78 | + q.offer(nodes[i].h); |
| 79 | + |
| 80 | + // 철로 시작점 ~ 회사(도착지) 거리가 d 안쪽인 경우 철로에 포함되는 것 |
| 81 | + while (!q.isEmpty() && nodes[i].o - q.peek() > d) { |
| 82 | + q.poll(); |
| 83 | + } |
| 84 | + ans = Math.max(ans, q.size()); |
| 85 | + } |
| 86 | + bw.write(ans + ""); |
| 87 | + bw.flush(); |
| 88 | + bw.close(); |
| 89 | + br.close(); |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | +``` |
0 commit comments