Maximum non-descending sub-sequence

Source: Internet
Author: User

Solution 1 (dichotomy: O (n*log2 (n))):

#include <stdio.h>
#include <iostream>
#define MAXN 10000
using namespace Std;

int A[MAXN], LOW[MAXN];

int main (void)
{
int n;
while (CIN >> N)
{
LOW[1]=A[1];
int now=1;
for (int i=1; i<=n; i++)
CIN >> A[i];
for (int i=2; i<=n; i++)
{
if (A[i]>low[now]) low[++now]=a[i];
Else
{
int Pos=lower_bound (Low, Low+now, A[i]) – low;
Low[pos]=a[i];
}
}
cout << now << Endl;
}
return 0;
}

Solution 2 (Dichotomy queue implementation: O (N*long2 (n))):

#include <stdio.h>
#include <iostream>
#include <string.h>
#define MAXN 1000000
using namespace Std;

int main (void)
{
int n;
while (CIN >> N)
{
int STACK[MAXN];
int top=0;
Stack[0]=-1;
for (int i=1; i<=n; i++)
{
int temp;
CIN >> Temp;
if (Temp>stack[top]) stack[++top]=temp;
Else
{
int Pos=lower_bound (stack, stack+top, temp)-stack;
Stack[pos]=temp;
}
}
cout << top << Endl;
}
return 0;
}

Solution 3 (O (n*n)):

#include <stdio.h>
#include <iostream>
#define MAXN 10000+10
using namespace Std;

int main (void)
{
int n;
while (CIN >> N)
{
int A[MAXN], alen[maxn], maxlen=1;
for (int i=1; i<=n; i++)
Alen[i]=1;
for (int i=1; i<=n; i++)
CIN >> A[i];
for (int i=2; i<=n; i++)
{
int max=0;
for (int j=1; j<=i-1; j + +)
if (A[j]<a[i] && max<alen[j]) max=alen[j];
alen[i]=max+1;
if (Alen[i]>maxlen) maxlen=alen[i];
}
cout << maxlen << Endl;
}
return 0;
}

Method 4 (DP (O (n*n))):

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define MAXN 100
using namespace Std;

int main (void)
{
int n;
while (CIN >> N)
{
int A[MAXN], LOW[MAXN];
memset (Low, 0, sizeof (low));
for (int i=0; i<n; i++)
CIN >> A[i];
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j + +)
{
if (A[j]>a[i])
low[j]++;
}

}
Sort (low, low+n);
cout << low[n-1];
}
return 0;
}

Maximum non-descending sub-sequence

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.