https://www.acwing.com/activity/content/problem/content/1114/1/
贪心思路:
把所有区间按照起点排序
遍历每个区间,找到可以进过st的最远距离,用双指针算法
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef pair<int,int> pll;
pll p[N];
int main(){
int st,se;
cin>>st>>se;
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>p[i].first>>p[i].second;
}
sort(p,p+n);
int res=0;
for(int i=0;i<n;i++){
int j=i,r=-2e9;
//找到最远的点;
while(j<n&& p[j].first<=st){
r=max(r,p[j].second);
j++;
}
//如果最远的点没有经过st 无解;
if(r<st){
res=-1;
break;
}
res ++ ;
if (r >= se) break;
st = r;
i = j -1;
}
cout<<res<<endl;
return 0;
}