题目描述
blablabla
样例
blablabla
算法1
Java 代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
// 对读入数据进行初始化
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(cin.readLine()); // 读取一行数据
// nums[0]存放区间左端点, nums[1]存放区间右端点
int nums[][] = new int[n][2];
for (int i = 0; i < n; i++) {
String[] strs = cin.readLine().split(" ");
nums[i][0] = Integer.parseInt(strs[0]);
nums[i][1] = Integer.parseInt(strs[1]);
}
int res = merge(nums);
System.out.println(res);
}
private static int merge(int[][] nums){
Arrays.sort(nums, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
int res = 0;
int r = Integer.MIN_VALUE;
for(int i = 0;i<nums.length;i++){
if(nums[i][0] > r){
res++;
}
r = Math.max(r, nums[i][1]);
}
return res;
}
}