Longest subsequence that first increases then decreases

好久不见. 提交于 2019-12-05 09:45:42

There are O(NlgK) algorithm for Longest Increasing Subsequence problem, where K is the LIS length. You can check Wikipedia for a description of the algorithm. LightOJ also has a nice tutorial (this might require login).

Edit: Oh, this answer is wrong. I missed the part about being able to delete elements to make longer conforming sequences. Still, for entertainment, here's a solution for the simple case where you don't get to delete elements:

I can think of an O(n) solution:

Walk the list once. Maintain some variables:

  • start of longest v-sequence seen
  • length of longest v-sequence seen
  • start of current v-sequence
  • current scan position
  • current scan state (ascending or descending)

Initialise both start pointers to the first element, the longest to zero, and the scan state to descending.

  1. Walk the list so long as numbers are descending and in the descending state.
  2. When an increasing number is encountered, change to ascending state and keep walking
  3. When the next decreasing number is encoutered, this is the end of a v-sequence.
  4. Compare with longest currently encountered v-sequence and update that if this one is longer.
  5. Reset start of current v-sequence and scan state to descending
  6. Walk the next sequence
  7. At end of array, return start and length of longest sequence.
simplest_coder

This one is a O(n) solution. Checked it on basic examples.
Let me know if it has any problem or if it does not work for any particular case.

CODE :

#include<stdio.h>
int max(int a,int b)
{
return (a >= b ? a : b);
}
int main()
{
    int i,j,n;
    scanf("%d",&n);
    int A[200022];
    int dec[200022]={0};
    int V[200022]={0};
    int state[200022]={0};
    for(i=0;i<n;i++)
    {
      scanf("%d",&A[i]);
    }
    if(A[0] > A[1])
        state[0]=1;
    for(i=1;i<n;i++)
    {
        j=i-1;
            if(A[i] < A[j])
            {    
                dec[i]=max(dec[i],dec[j]+1);
                V[i]=max(V[i],V[j]);
                state[i]=1;
            }    
            else if(A[i] == A[j])
            {    
                dec[i]=dec[j];
                V[i]=V[j];
                state[i]=state[j];
            }
            else
            {
                if(state[j]==1)
                {
                    dec[i]=dec[i];
                    V[i]=max(V[i],dec[j]+1);
                    V[i]=max(V[i],V[j]+1);
                    state[i]=1;
                }
                else
                {
                    dec[i]=dec[i];
                    V[i]=max(V[i],V[j]);
                }
            }

//  printf("%d %d\n",dec[i],V[i]);
}
    if(V[n-1] == 0)
        printf("0\n");
    else
        printf("%d\n",V[n-1]+1);
 }
Ishmeet Singh

Construct array inc[i] where inc[i] stores Longest Increasing subsequence ending with A[i]. Construct array dec[i] where dec[i] stores Longest Decreasing subsequence ending with A[i].

then find the maximum value of (inc[i] + dec[i] - 1)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!