After all these sort of one side, the other side were class dp
Define an array c, c [i] denotes the i all combinations of routes can be opened, the minimum value of the maximum number of position number piece route
For example, following a sample
1 3
2 4
3 1
4 2
At this position numbers for the left route is already sorted up
You need only consider the case of the right
In all combinations opened only in a channel
Result appears to the right may be {1}, {2}, {3}, {4}
Therefore, c [1] = 1
In all combinations of two channel opened only in
Result appears to the right may be {3,4}, {1,2}
There are two combinations, each combination of the position of the maximum number of elements 2 and 4, respectively, whichever is less
To give c [2] = 2
We can find an array of c is strictly increasing
C then defined over an array, what use is it to say next
Because the routes left sorted, consider the value of each is the right number
For example again
1 4
2 5
3 1
4 2
5 3
Take only right border sequence, of the object to obtain dp 45123
Consider 4, to give c [1] = 4, the answer is updated to 1
5 to consider, because 5> 4, left and incremented, this route can be obtained and the end of route 4 do not overlap, may be combined, so that c [2] = 5, the answer is updated to 2
Consider 1, because there is no element of c is smaller than an array, the update c [1] = 1, the same answer, 2
Consider 2 to give c [1] <2, and at this time can be c [1] refers to a combination of routes, update c [2] = 2, the answer unchanged at 2
Consider 3, to give c [2] <3, and can be a combination of these two case c [2] refers to, c [3] = 3, the answer is updated to 3
First, the maximum number, the answer is to be combined together, that is the greatest array subscript c
So why this update can do?
Formally, updated after the array is equivalent to the destruction of the previous route conclusion (look in front of the route will cross)
In fact, because every consideration to a point of time, can be seen as a combination of the previous point can have a combination of finished
C update the elements of the array, in order to make the point after the greedy add to the mix a greater contribution to the answer, let the point after to have a better combination
For the code to achieve, access to the function or manually lower_bound analog binary search (known in the prior are ordered array c)
1 /* 2 Written By StelaYuri 3 */ 4 #include<iostream> 5 #include<algorithm> 6 #include<cstring> 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 const int m=100010; 10 struct line{ 11 int a,b; 12 bool operator < (const line& x) const{ 13 return a<x.a; 14 } 15 }r[m]; 16 int c[m]; 17 int main(){ 18 ios::sync_with_stdio(0); 19 cin.tie(0);cout.tie(0); 20 int T,n,i,p,ans; 21 cin>>T; 22 while(T--){ 23 cin>>n; 24 for(i=1;i<=n;i++) 25 cin>>r[i].a>>r[i].b; 26 sort(r+1,r+1+n); 27 memset(c,INF,sizeof c); 28 ans=0; 29 for(i=1;i<=n;i++){ 30 p=lower_bound(c+1,c+1+ans,r[i].b)-c; 31 ans=max(ans,p); 32 c[p]=r[i].b; 33 } 34 cout<<ans<<endl; 35 } 36 37 return 0; 38 }