Poj 1887 testingthe catcher (LIS: longest descent subsequence)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.