Longest ascending subsequence

Source: Internet
Author: User
Longest ascending subsequence

C + + STL vector max and Min min_element (V.begin (), V.end ())
Max_element (V.begin (), v.end ()) sizeof (n)/sizeof (int) min_element
The algorithm returns the position of the smallest element in the sequence [first, last].

Problem Description

A number of sequence AI, when a 1 < a 2 < ... < a S, we call this sequence ascending. For a given sequence (a 1, a 2,
..., a N), we can get some ascending subsequence (a i1, a i2, ..., a iK), here 1 <= i1 "seek a K (K=1, 2, 3 ... N) The length of the longest ascending subsequence of the end point "the rightmost number of a ascending subsequence, called the" end point "of the subsequence.

**
Although this sub-problem and the original problem form is not exactly the same, but as long as the N sub-problem is solved, then the solution of the N sub-problem, the biggest one is the solution of the whole problem.

2. Determine the status:
Sub-problems are related only to one variable – the position of the number. So the position K of the number in the sequence is the "state", and the "value" corresponding to the state k is the length of the longest ascending subsequence with a k as the "end point". There are a total of n states.

3. Find the state transition equation:
MaxLen (k) indicates the length of the longest ascending subsequence with a k as the "end point" then: initial state:

MaxLen (1) = 1 
maxlen (k) = max {MaxLen (i): 1<=i < K and a I < a K and k≠1} + 1

If no such i is found, then the value of MaxLen (k) = 1 MaxLen (k) is on the left side of a K, the "end point" value is less than a k, and the length of the ascending sub-sequence with the largest length plus 1. Since any "end point" on the left side of a k is smaller than the subsequence of a k, a longer ascending sequence can be formed after a k is added.

"Everyone for Me" recursive type of motion return program

#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX 1010
using namespace std;
int A[max], Maxlen[max];

int main ()
{
    int N;
    Cin >> N;
    for (int i=1; i<=n; i++)
    {
        cin >> a[i];
        Maxlen[i] = 1;
    }
    for (int i=2, i<=n; i++)
    {for
        (int j=1; j<i; j + +)
        {
            if (A[i] > A[j])
                maxlen[i] = max ( Maxlen[i], maxlen[j]+1);//note here to take maxlen[i] and maxlen[j]+1 maximum if not, see picture Consequences
        }
    }
    cout << *max_ Element (maxlen+1, maxlen+n+1);
    return 0;
}

corresponds to 51NOD 1134

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define N 50010

int a[n];
int b[n];

int upper_bound (int len, int A)
{
    int i;
    for (i=0; i<len; i++)
    {
        if (B[i] > A)
            return i;
    }
    return i;
}
int List (int n)
{
    b[0] = a[0];
    int Len =1;
    for (int i=1; i<n; i++)
    {
        b[a[i]>b[len-1]? len++:upper_bound (Len, a[i])] = A[i];
    }
    return len;
}
int main ()
{
    int n;
    scanf ("%d", &n);
    for (int i=0; i<n; i++)
        scanf ("%d", &a[i]);
    printf ("%d", List (n));
    return 0;
}

C + + has a similar STL

The Lower_bound (first, last, Val) algorithm returns the position of a non-descending sequence in the order of a value greater than or equal to Val.

The Upper_bound (first, last, Val) algorithm returns a non-descending sequence of the position greater than the Val in the last.

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.