First sort according to the first number, and then you can get a series of the second number of sequences, because the first one from the big to the small arrangement, so the second set of data, the latter can not be smaller than the previous small to not cross, then that is to find the longest common sub-sequence of this new sequence
Here to use the longest ascending subsequence of the NLOGN algorithm, create a new array to save all reasonable data of the array g, such as the G array with 1,4,6, add a 3, then 4 can be replaced by 3 because the same position as small as possible to accommodate the number of data will be more, the more can find a longer sequence, The process of finding a position is to search for the complexity of the logn with two points.
Note that I have not done this for a long time. When the number of edges is greater than 1, the road will become plural roads
Besides, this is the two points in the main function of someone else's direct writing, and I think it's a lot easier than mine.
ANS[1]=NUM[1];
Len=1;
for (i=2;i<=n;i++)
{
/*****///This dichotomy is written by someone else, which means much better than I wrote-I.
Low=1;
Up=len;
while (Low<=up)
{
Mid= (Low+up)/2;
if (Ans[mid]<num[i]) low=mid+1;
else up=mid-1;
}
Ans[low]=num[i];
if (Low>len) len++;
/*****/
}
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 7 Const intMAXN =500005;8 intNUM[MAXN], G[MAXN];//The g array is stored in a sequential sequence9 Ten structpair{ One intx, y; A BOOL operator< (ConstPair &m)Const{ - returnX <m.x; - } the }P[MAXN]; - - intBin_search (intMintk) - { + intSt =0, La = k, ans =0; - intmid; + while(St <=LA) { AMid = (st + LA)/2; at if(M > G[mid] && m <= G[mid +1]){ -Ans = mid +1; - Break; - } - Else if(M <= g[mid]) La = mid-1; - ElseSt = Mid +1; in } - returnans; to } + - intMain () the { * //freopen ("a.in", "R", stdin); $ intN, cas =0;Panax Notoginseng while(SCANF ("%d", &n)! =EOF) { - for(inti =0; I<n; i++){ thescanf"%d%d", &p[i].x, &p[i].y); + } ASort (p, p+n); the + for(inti =0; I<n; i++) -num[i+1] =p[i].y; $ $ intK =0; -g[0] =0; - for(inti =1; I<=n; i++){ the if(Num[i] > G[k]) g[++k] =Num[i]; - Else{Wuyi intpos =Bin_search (Num[i], k); theG[pos] =Num[i]; - } Wu } - //There's more than one way, the plural roads!!! About if(k = =1) printf ("Case %d:\nmy King, on most%d road can be built.\n", ++CAs, k); $ Elseprintf"Case %d:\nmy King, at the most%d roads can be built.\n", ++CAs, k); -Puts""); - } - return 0; A}
HDU 1025 Longest Ascending subsequence