ybt 1324 整数区间

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
struct number{
	int x,y;
}stu[10010];
int cmp(number x1,number x2){
	return x1.y<x2.y;//按右侧排序
}
int main() {
    int i,n,count=0;
    i=0;
    scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&stu[i].x,&stu[i].y);
	}
	sort(stu,stu+n,cmp);
	int w=-1;
		for(i=0;i<n;i++){
		if(stu[i].x>w){//下一组的左侧大于本组的右侧 假如i=2,即第三组且if成立,有两种可能 1.与第二组有交集2.与第二组无交集 无论哪种都与第一种无交集所以要另选数字
			w=stu[i].y;//将该组的右侧定位选取区间中间的数
			count++;
		}
			
	}
	printf("%d",count);
    return 0;
}

 

posted @ 2022-04-24 22:13  半喜  阅读(45)  评论(0)    收藏  举报