注意按照右端点排序的语句:list接收返回_Collectors.toList()__getR____collect
list=list.stream().sorted(Comparator.comparing(Pair::getR)).collect(java.util.stream.Collectors.toList());
class Pair和class Main并列,即不可同时两个public class
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
List<Pair> list=new ArrayList<>();
for(int i=0;i<n;i++){
list.add(new Pair(sc.nextInt(),sc.nextInt()));
}
list=list.stream().sorted(Comparator.comparing(Pair::getR)).collect(java.util.stream.Collectors.toList());
int tar=list.get(0).getR(),res=1;
for(int i=1;i<list.size();i++){
int t=list.get(i).getL();
if(t>tar) {tar=list.get(i).getR();res++;}
}
System.out.println(res);
}
}
class Pair
{
int l;
int r;
Pair(int l,int r){this.l=l;this.r=r;}
public int getL(){return l;}
public int getR(){return r;}
}