UV 10534 wavio sequence (longest incrementing sub-sequence DP binary)

Source: Internet
Author: User

Wavio Sequence


Wavio is a sequence of integers. It has some interesting properties.

· Wavio is of odd length I. e.L = 2 * n + 1.

· The first(N + 1)Integers of wavio sequence makes a strictly increasing sequence.

· The Last(N + 1)Integers of wavio sequence makes a strictly decreasing sequence.

· No two adjacent integers are same in a wavio sequence.

For example1, 2, 3, 4, 5, 4, 3, 2, 0Is an wavio sequence of Length9.1, 2, 3, 4, 5, 4, 3, 2, 2Is not a valid wavio sequence. in this problem, you will be given a sequence of integers. you have to find out the length of the longest wavio sequence which is a subsequence of the given sequence. consider, the given sequence:

1 2 3 2 1 2 3 3 3 2 1 5 4 1 2 2 1.


Here the longest wavio sequence is:1 2 3 4 5 4 3 2 1. So, the output will be9.

 

Input

The input file contains less75Test cases. The description of each test case is given below: input is terminated by end of file.

 

Each set starts with a postive integer,N (1 <= n <= 10000). In next few lines there will beNIntegers.

 

Output

For each set of input print the length of longest wavio sequence in a line.

Sample input output for sample input
10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
 
9
9
1

 

Problemsetter: Md. kamruzzaman, member of elite problemsetters 'panel


The maximum value of the maximum values in the longest ascending sequence (LIS) and longest descending sequence (LDS) of a sequence.

I started to write it directly with DP, And then it timed out. Later I saw someone saying that I would use binary to optimize the time complexity to O (N * logn ).

Use a stack s to save the smallest tail of LIS with the length of I. s [I] top is the top of the stack, that is, the length of the current Lis. Initial s [1] = A [1] Top = 1 Traversal whole sequence when a [I]> S [Top], a [I] goes into the stack in [I] = Top

Otherwise, search for the first subscript POS with a value greater than or equal to a [I] in the stack and replace it, which increases the growth potential of LIS in [I] = Pos;

In [I] indicates the length of lis at the end of a [I;

The process of finding the LDs is similar.


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 10005;int a[N], in[N], de[N], s[N], m, n, top, pos;int BinSearch (int k, int le, int ri){    while (le <= ri)    {        m = (le + ri) >> 1;        if (s[m] >= k) ri = m - 1;        else le = m + 1;    }    return ri + 1;}void lis(){    memset (s, 0, sizeof (s));    memset (in, 0, sizeof (in));    s[1] = a[1];    in[1] = top = 1;    for (int i = 2; i <= n; ++i)    {        if (s[top] < a[i])        {            s[++top] = a[i];            in[i] = top;        }        else        {            pos = BinSearch (a[i], 1, top);            s[pos] = a[i];            in[i] = pos;        }    }}void lds(){    memset (s, 0, sizeof (s));    memset (de, 0, sizeof (de));    s[1] = a[n];    de[n] = top = 1;    for (int i = n - 1; i >= 1; --i)    {        if (s[top] < a[i])        {            s[++top] = a[i];            de[i] = top;        }        else        {            pos = BinSearch (a[i], 1, top);            s[pos] = a[i];            de[i] = pos;        }    }}int main(){    while (scanf ("%d", &n) != EOF)    {        for (int i = 1; i <= n; ++i)            scanf ("%d", &a[i]);        int ans = 1;        lis();        lds();        for (int i = 1; i <= n; ++i)        {            if (min (de[i], in[i]) > ans)                ans = min (de[i], in[i]);        }        printf ("%d\n", ans * 2 - 1);    }    return 0;}


And the DP version of TLE.

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=10005;int a[N],c[N],d[N],n;int dpde(int i){    if(d[i]) return d[i];    d[i]=1;    for(int j=i;j<=n;++j)    {        if(a[i]>a[j])            d[i]=max(d[i],dpde(j)+1);    }    return d[i];}int dpin(int i){    if(c[i]) return c[i];    c[i]=1;    for(int j=i;j>=1;--j)    {        if(a[i]>a[j])            c[i]=max(c[i],dpin(j)+1);    }    return c[i];}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i=1;i<=n;++i)            scanf("%d",&a[i]);        int ans=1;        memset(d,0,sizeof(d));        memset(c,0,sizeof(c));        for(int i=1;i<=n;++i)        {            if(min(dpde(i),dpin(i))>ans)                ans=min(d[i],c[i]);        }        printf("%d\n",ans*2-1);    }    return 0;}


Zookeeper

UV 10534 wavio sequence (longest incrementing sub-sequence DP binary)

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.