AcWing 907. 区间覆盖
原题链接
简单
作者:
I+III
,
2021-05-30 14:21:57
,
所有人可见
,
阅读 232
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 100005;
PII p[N];
int main(){
int s, t, n, cnt = 0;
cin>>s>>t;
cin>>n;
for(int i = 0; i < n; i++){
cin>>p[i].first>>p[i].second;
}
sort(p, p + n);
for(int i = 0; i < n && s < t; i++){
int st = s;
while(i < n && p[i].first <= s){
st = max(st, p[i].second);
i++;
}
if(s == st){
cnt = -1;
break;
}
s = st;
cnt++;
i--;
}
if(s < t) cnt = -1;
cout<<cnt<<endl;
return 0;
}