Poj 1887 testingthe catcher (LIS: longest descent subsequence)
Http://poj.org/problem? Id = 3903
Question:
Here is a numerical sequence whose length is n (n <= 200000). You need to calculate the longest (strict) Length of the sequence to decrease the length of the subsequence.
Analysis:
Read all input values, reverse the original array, and obtain the maximum number of strictly ascending subsequences.
Since N is 20 W, we can only use the O (nlogn) algorithm.
G [I] = X indicates that the length of the current traversal isAllIn the longest ascending subsequenceMinimumThe end value of the sequence is X. (if so far there is no ascending sequence of long I, then x = inf infinity)
Assume that the J-th value (A [J]) is traversed. First, find the bottom confirmation of the value of a [J] of the G [N] array (That is, the K value of G [k] of the first value> = A [J]). It indicates that there is a longest ascending subsequence with a length of K-1 and the position at the end of the sequence <J and the end value of the sequence <a [J].
Then we can set G [k] = A [J] and DP [I] = K (DP meaning as solution 1 ).
(The above section takes time to carefully understand)
In the end, we can find the largest I of the subscript so that: G [I] <INF has the largest I subscript. This I is the length of LIS.
AC code: O (N * logn) Complexity
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=200000+5;const int INF=1e8;int n;int a[maxn];int g[maxn];int main(){ int kase=0; while(scanf("%d",&a[1])==1 && a[1]!=-1) { if(kase>0) printf("\n"); n=2; while(scanf("%d",&a[n])==1 && a[n]!=-1) { n++; } n--; reverse(a+1,a+n+1); for(int i=1;i<=n;i++) g[i]=INF; int ans=0; for(int i=1;i<=n;i++) { int k=lower_bound(g+1,g+n+1,a[i])-g; g[k]=a[i]; ans=max(ans,k); } printf("Test #%d:\n maximum possible interceptions: %d\n",++kase,ans); } return 0;}
Poj 1887 testingthe catcher (LIS: longest descent subsequence)