最长上升子序列 POJ2533

Description

A numeric sequence of ai is ordered if a1 < a2 <
... < aN. Let the subsequence of the given numeric sequence (a1, a2,
..., aN) be any sequence (ai1, ai2,
..., aiK), where 1 <= i1 < i2 <
... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others.
All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4
#include <cstdio>
#include <memory.h>
#define MAX_N 1000
int b[MAX_N+10];
int aMaxLen[MAX_N+10] ;
void main()
{
	int N,i;
	while(scanf("%d",&N))
	{
		for (i=1;i<=N; ++i)
		{
			scanf("%d",&b[i]);
		}
		aMaxLen[1]=1;
		for (i=2;i<=N;++i)
		{
			int nTmp = 0;
			for (int j = 1;j<i;++j)
			{
				if (b[i]>b[j])
				{
					if (nTmp < aMaxLen[j])
					{
						nTmp = aMaxLen[j] ;
					}
				}
			}
			aMaxLen[i] = nTmp + 1 ;
		}
		/*for (i=0;i<=N;++i)
		{
			printf("%d ",aMaxLen[i]);
		}
		printf("\n");*/
		int nMax = -1;
		for (i=1;i<=N;++i)
		{
			if (nMax<aMaxLen[i])
			{
				nMax =aMaxLen[i] ;
			}
		}
		printf("%d\n",nMax);
	}
}

时间: 2024-10-11 08:55:23

最长上升子序列 POJ2533的相关文章

poj2533——lis(最长上升子序列), 线性dp

poj2533——lis(最长上升子序列), 线性dp Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36143   Accepted: 15876 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given n

POJ-2533最长上升子序列(DP+二分)(优化版)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

poj-2533 Longest Ordered Subsequence 【最长上升子序列】

题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35929   Accepted: 15778 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the

POJ2533 最长上升子序列

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define sc(x) scanf("%d",&(x)) 6 #define pf(x) printf("%d\n", x) 7 using namespace std; 8 const int MAX = 1005; 9 int a

最长上升子序列LIS解法(n^n &amp;&amp; nlogn)

最长递增子序列问题 在一列数中寻找一些数满足 任意两个数a[i]和a[j] 若i<j 必有a[i]<a[j] 这样最长的子序列称为最长递增子序列LIS LIS问题有两种常见的解法 一种时间复杂度n^n 一种时间复杂度nlogn 下面我们先来说一下n^n的算法 设dp[i]表示以i结尾的最长上升子序列的长度 把问题分解 分解成序列中每一项最为终点的最大上升子序列 从第二项开始依次判断 最后找出最大的一项就是答案 则状态转移方程为 dp[i] = max{dp[j]+1}, 1<=j<

【最长上升子序列LIS】O(n^2)和O(nlogn)算法简记

最长上升子序列(Longest Increasing Subsquence)是指对一个序列,其中满足i < j < k且a[i] < a[j] < a[k]的最长子序列a[].比如1 4 2 6 3 7 9,则[1,2,3,7,9]就是它的LIS. LIS普遍求法为动态规划.有两种算法. 第一种比较好写,复杂度O(n^2). 设原序列为a[].所有下标从1开始(即[1,n]).定义dp[i]为以a[i]结尾的最长上升子序列的长度.很容易得到转移方程:dp[i] = max{1, d

14-高效求最长公共子序列(二维数组存不下)

/*                                   See LCS again时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述 There are A, B two sequences, the number of elements in the sequence is n.m; Each element in the sequence are different and less than 100000. Calculate the length

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

最长公共子序列的代码实现

关于最长公共子序列(LCS)的相关知识,http://blog.csdn.net/liufeng_king/article/details/8500084 这篇文章讲的比较好,在此暂时不再详说. 以下是我代码实现两种方式:递归+递推: 1 #include <bits/stdc++.h> 2 using namespace std; 3 int A[100]; 4 int B[100]; 5 6 //int B[]={2,3,5,6,9,8,4}; 7 int d[100][100]={0};