Nlogn solution of longest ascending subsequence sequence in DP practice

Source: Internet
Author: User

Openjudge 2757: Longest ascending sub-sequence
Total time limit:
2000ms
Memory Limit:
65536kB
Describe
A number of sequence bi, when B1 < B2 < ... < BS , we call this sequence ascending.  For a given sequence ( A1, A2, ..., an), We can get some ascending sub-sequences ( ai1, ai2, ..., AiK), here 1 <= i1 < i2 < ... < IK <= N. For example, for sequences (1, 7, 3, 5, 9, 4, 8), there are some ascending sub-sequences of it, such as (1, 7), (3, 4, 8) and so on. The longest length of these subsequence sequences is 4, such as subsequence (1, 3, 5, 8).

Your task is to find the length of the longest ascending sub-sequence for a given sequence.
Input
The first line of input is the length of the sequence n (1 <= n <= 1000). The second line gives the n integers in the sequence, with values ranging from 0 to 10000.
Output
The length of the longest ascending sub-sequence.
Sample input
71 7 3 5 9 4 8
Sample output
4
1 /*Do this problem again because another topic is to reverse the use of this C array, here to review*/2#include <iostream>3 using namespacestd;4#include <cstdio>5#include <cstring>6 intN;7 #defineN 10108 intc[n],f[n],ans=-N,a[n];9 intSearchintLintRintk)Ten { One     if(L==R)returnl; A     intMid= (l+r+1) >>1;/*and +1 here.*/ -     if(c[mid]>=k)returnSearch (l,mid-1, k);/*the mid-1 here is guaranteed to be ascending sequence*/ -     Else returnsearch (mid,r,k); the } - intMain () - { -scanf"%d",&n); +      for(intI=1; i<=n;++i) -scanf"%d",&a[i]); +Memset (F,0,sizeof(f)); AMemset (c,127,sizeof(c)); at      for(intI=1; i<=n;++i) -     { -F[i]=search (0, I,a[i]) +1;/*the specific two-part process here is best to do a manual simulation to prevent errors*/ -c[f[i]]=min (A[i],c[f[i]]); -ans=Max (F[i],ans); -     } inprintf"%d\n", ans); -     return 0; to}

Nlogn solution of longest ascending subsequence sequence in DP practice

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.