AcWing 803. JAVA区间合并
原题链接
简单
作者:
ARM
,
2020-08-05 11:59:56
,
所有人可见
,
阅读 569
java 代码
//右区间值大,就合并,然后更新右值
import java.io.*;
import java.lang.Integer;
import java.util.*;
class Node{
int first;
int second;
public Node(int first, int second){
this.first = first;
this.second = second;
}
}
class Main{
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(buf.readLine());
Node[] nodes = new Node[n];
for(int i = 0; i < n; ++i){
String[] params = buf.readLine().split(" ");
int x = Integer.valueOf(params[0]);
int y = Integer.valueOf(params[1]);
nodes[i] = new Node(x, y);
}
Arrays.sort(nodes, (p,q)->{return p.first - q.first;});
int cnt = 0;
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; ++i){//只比较右值,包含在里面则合并,没包含则加1
if(max < nodes[i].first)cnt++;
max = Math.max(max, nodes[i].second);
}
System.out.printf("%d", cnt);
}
}