题意:vj上有中文版
思路:见代码;
注意剪枝就行了这题。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+50;
int t,n,st,ed,cnt,maxr;
struct COW{
int l,r;
}c[maxn];
bool cmp(COW a,COW b){
if(a.l==b.l) return a.r>b.r;
return a.l<b.l;
}
int solve(){
if(st>1){
return -1;
}///最小值大于1无解
if(ed>=t) return cnt;
int i=1;
while(true){
for(;i<n&&c[i].l<=ed+1;i++)///剪枝 这里注意i不用从头开始,因为前面走过了就不用再走
maxr=max(maxr,c[i].r);///找到大最右端点
if(maxr<=ed) return -1;
cnt++,ed=maxr;
if(ed>=t) return cnt;
}
return -1;
}
int main(){
while(scanf("%d%d",&n,&t)!=EOF){
memset(c,0,sizeof(c));
cnt=1;
for(int i=0;i<n;i++)
scanf("%d%d",&c[i].l,&c[i].r);
sort(c,c+n,cmp);
st=c[0].l,maxr=ed=c[0].r;
printf("%d\n",solve());
}
return 0;
}