AcWing 905. 区间选点
原题链接
简单
作者:
古傲狂生强
,
2021-06-04 18:57:00
,
所有人可见
,
阅读 219
区间选点 ===== java 题解
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Range{
public int l,r;
public Range(int l,int r){
this.l = l;
this.r = r;
}
@Override
public String toString() {
return "Range [l=" + l + ", r=" + r + "]";
}
}
public class Main {
static int N = 100010;
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Range []ranges = new Range[n];
for (int i= 0; i < ranges.length; i++){
ranges[i] = new Range(0,0);
}
for(int i = 0; i < n; i++){
int xl = sc.nextInt();
int xr = sc.nextInt();
ranges[i] = new Range(xl,xr);
}
//实现排序接口
Comparator<Range> cmp = new Comparator<Range>() {
@Override
public int compare(Range o1, Range o2) {
if(o1.r < o2.r){
return -1;
}else if(o1.r > o2.r){
return 1;
}else{
return 0;
}
}
};
Arrays.sort(ranges,cmp);
int res = 0;
double ed = -2e9;
for(int i = 0; i < n; i++){
if(ranges[i].l > ed){
res++;
ed = ranges[i].r;
}
}
System.out.println(res);
}
}