fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Main {
  7. static int N;
  8.  
  9. static PriorityQueue<Class> classes;
  10. static PriorityQueue<Class> pq;
  11. static StringTokenizer st;
  12.  
  13. public static void main(String[] args) throws IOException {
  14. init();
  15. process();
  16. print();
  17. }
  18.  
  19. private static void print() {
  20. System.out.println(pq.size());
  21. }
  22.  
  23. private static void process() {
  24.  
  25. for (Class c : classes) {
  26.  
  27. if (pq.isEmpty()) {
  28. pq.add(c);
  29. continue;
  30. }
  31.  
  32. Class earlyEndClass = pq.peek();
  33.  
  34. // 강의실을 새로 배정해야하는 경우, (현재 진행하는 강의 중 이어서 할 수 있는 강의실이 없는 경우)
  35. if (earlyEndClass.end <= c.start) {
  36. pq.poll();
  37. }
  38.  
  39. pq.add(c);
  40. }
  41. }
  42.  
  43. public static void init() throws IOException {
  44. classes = new PriorityQueue<>(new Comparator<Class>() {
  45. @Override
  46. public int compare(Class o1, Class o2) {
  47. if (o1.start != o2.start) {
  48. return o1.start - o2.start;
  49. }
  50.  
  51. return o1.end - o2.end;
  52. }
  53. });
  54.  
  55. pq = new PriorityQueue<>(new Comparator<Class>() {
  56. @Override
  57. public int compare(Class o1, Class o2) {
  58. return o1.end - o2.end;
  59. }
  60. });
  61.  
  62. N = Integer.parseInt(br.readLine());
  63.  
  64. for (int i = 0; i < N; i++) {
  65. st = new StringTokenizer(br.readLine());
  66.  
  67. int start = Integer.parseInt(st.nextToken());
  68. int end = Integer.parseInt(st.nextToken());
  69.  
  70. classes.add(new Class(start, end));
  71. }
  72. }
  73.  
  74.  
  75. public static class Class {
  76. int start;
  77. int end;
  78.  
  79. public Class(int start, int end) {
  80. this.start = start;
  81. this.end = end;
  82. }
  83. }
  84.  
  85. }
Success #stdin #stdout 0.09s 53092KB
stdin
3
5 6
3 4
1 2
stdout
2