题目描述
blablabla
样例
blablabla
参考链接:https://blog.csdn.net/sinat_38816924/article/details/82934900
java 代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static class Interval implements Comparable<Interval>{
int a;
int b;
public Interval(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Interval y) {
if(this.b!=y.b)
return (this.b<y.b)?-1:1;
if(this.a!=y.a)
return (this.a<y.b)?-1:1;
return 0;
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int count=1;
Interval arr[]=new Interval[n];
for (int i = 0; i < n; i++) {
arr[i]=new Interval(scanner.nextInt(),scanner.nextInt());
}
Arrays.sort(arr);
int newend=arr[0].b;
for (int i = 1; i <n; i++) {
if (newend<arr[i].a){
count++;
newend=arr[i].b;
}
}
System.out.println(count);
}
}