Description
给出n个区间的起点和终点,求最少使用其中多少个区间可以将所有区间所在的区域完全覆盖。(测试的数据确保这1点)。
Input
第1行一个整数n,表示n个区间;
第2行开始n行,每行2个整数,表示一个区间范围。
Output
按区间先后顺序,输出选中的区间。
Sample Input
7
1 5
1 6
3 6
1 7
6 9
9 10
7 9
Sample Output
1 7
6 9
9 10
算法思想
- 将区间按照左端点从小到大排序,左端点相同按右端点从小到大排序
- 每次都找到左端点在已覆盖区域内,右端点尽可能长的区间
#include<iostream>
#include<algorithm>
using namespace std;
class Section{
public:
int num1;
int num2;
};
// 根据区间的左端点进行升序排序
// 左端点一致的根据右端点升序排序
bool cmp(Section s1, Section s2){
if(s1