본문 바로가기
알고리즘 & 자료구조/백준

백준 11000

by 신재권 2022. 7. 17.
package baekjoon.그리디;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main11000 {

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
      int N = Integer.parseInt(br.readLine());
      Pair[] a = new Pair[N];
      PriorityQueue<Integer> pq = new PriorityQueue<>();

      for (int i = 0; i < N; i++) {
         st = new StringTokenizer(br.readLine());
         a[i] = new Pair(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
      }
      Arrays.sort(a);

      pq.add(a[0].e);
      for (int i = 1; i < N; i++) {
         if (pq.peek() <= a[i].s) {
            pq.poll();
         }
         pq.add(a[i].e);
      }
      System.out.println(pq.size());
   }

   private static class Pair implements Comparable<Pair> {
      int s, e;

      public Pair(int s, int e) {
         this.s = s;
         this.e = e;
      }

      @Override
      public int compareTo(Pair o) {
         if (this.s != o.s) {
            return this.s - o.s;
         }
         return this.e - o.e;
      }
   }
}

 

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 10610  (0) 2022.07.28
백준 1789  (0) 2022.07.27
백준 1449  (0) 2022.07.17
백준 16953  (0) 2022.07.17
백준 10162  (0) 2022.07.16