AcWing 905. Interval selection point
1. Topic
Given N closed intervals [ai,bi], please select as few points as possible on the number axis, so that each interval contains at least one selected point.
Output the minimum number of points selected.
Points that lie on the endpoints of the interval are also counted as being within the interval.
input format
The first line contains the integer N, representing the number of intervals.
Next N lines, each line contains two integers ai, bi, representing the two endpoints of an interval.
output format
Output an integer representing the minimum number of points required.
data range
5 times of 1≤N≤10,
9 times of −10≤ai≤bi≤109 times
Input sample:
3
-1 1
2 4
3 5
Sample output:
2
AcWing 905. Interval selection point
2. Ideas
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int N=100010;
static int [][]he=new int[N][2];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for (int i = 0; i < n; i++) {
he[i][0]=sc.nextInt();
he[i][1]=sc.nextInt();
}
//按左端点大小冒泡排序
Arrays.sort(he,0,n,(a,b)->(a[0]-b[0]));
//从最左边的区间开始依次遍历,这个点是否包含在下一个区间,不含则要增加一个点并更新
int l=he[0][0];
int r=he[0][1];
int res=1;
for (int i = 1; i < n; i++) {
if(he[i][0] >r){
res++;
l=he[i][0];
r=he[i][1];
}else r=Math.min(r,he[i][1]);
}
System.out.println(res);
}
}
AcWing 908. Maximum number of disjoint intervals
AcWing 908. Maximum number of disjoint intervals
Maximum number of disjoint intervals == minimum number of covered interval points
Because if several intervals can be covered by the same point,
it means that they intersect,
so there are several points that have several disjoint intervals
Thank you for reading it. If it is helpful to you, please give it a thumbs up and support it.