-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoj2376.cpp
More file actions
64 lines (61 loc) · 947 Bytes
/
Copy pathpoj2376.cpp
File metadata and controls
64 lines (61 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<stdio.h>
using namespace std;
struct COW{
int start,end;
}cows[25000];
void qsort(int l,int r){
if(l>=r)
return;
int i=l;
int j=r+1;
COW key=cows[l];
while(i<j){
while(i<j&&cows[--j].start>=key.start);
cows[i]=cows[j];
while(i<j&&cows[++i].start<=key.start);
cows[j]=cows[i];
}
cows[i]=key;
qsort(l,i-1);
qsort(i+1,r);
}
int main() {
int n,e;
scanf("%d%d",&n,&e);
for(int i=0;i<n;i++){
scanf("%d%d",&cows[i].start,&cows[i].end);
}
qsort(0,n-1);
int now=0;
int ans=0;
int scan=0;
bool isCover;
while(now<e){
int max=0;
int maxIndex=0;
isCover=false;
if(scan>=n)
break;
for(;scan<n;scan++){
if(cows[scan].start>now+1){
break;
}
if(cows[scan].end>max){
max=cows[scan].end;
maxIndex=scan;
isCover=true;
}
}
if(!isCover)
break;
ans++;
now=cows[maxIndex].end;
}
if(isCover){
cout<<ans<<endl;
}else{
cout<<-1<<endl;
}
return 0;
}