AcWing 803. 区间合并

时间:2024-03-05 08:05:20
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static StreamTokenizer sr = new StreamTokenizer(in); static int n; static List<int[]> list = new ArrayList<>(); public static void main(String[] args) throws IOException { n = nextInt(); for (int i = 0, l, r; i < n; i++) { l = nextInt(); r = nextInt(); list.add(new int[] { l, r }); } Collections.sort(list, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { // TODO Auto-generated method stub return o1[0] - o2[0]; } }); int ans = 0; int r = Integer.MIN_VALUE; for (int[] arr : list) { if(arr[0] > r) { ans++; } r = Math.max(r, arr[1]); } out.println(ans); out.flush(); } private static int nextInt() throws IOException { sr.nextToken(); return (int) sr.nval; } }