poj 2533 最长上升子序列

Longest Ordered Subsequence

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39374   Accepted: 17315

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

Source

Northeastern Europe 2002, Far-Eastern Subregion

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

int a[10000];
int dp[10000];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            for(int j=i-1; j>=1; j--)
            {
                if(a[i]>a[j])
                {
			<span id="transmark"></span>dp[i]=max(dp[i],dp[j]+1);
                }
            }
        }
        int Max=-1;
        for(int i=1;i<=n;i++) //不一定dp[n]最大
			if(Max<dp[i])
				Max=dp[i];
        printf("%d\n",Max+1);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-11 21:47:43

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

poj 3903 &amp; poj 2533 最长上升子序列(LIS)

最长上升子序列. 做这道题之前先做了2533,再看这道题,感觉两道题就一模一样,于是用2533的代码直接交, TLE了: 回头一看,数据范围.2533 N:0~1000:3903 N :1~100000. 原因终归于算法时间复杂度. 也借这道题学习了nlgn的最长上升子序列.(学习链接:http://blog.csdn.net/dangwenliang/article/details/5728363) 下面简单介绍n^2 和 nlgn 的两种算法. n^2: 主要思想:DP: 假设A1,A2..

[kuangbin带你飞]专题十二 基础DP1 N - Longest Ordered Subsequence POJ - 2533(最长上升子序列LIS)

N - Longest Ordered Subsequence POJ - 2533 题目链接:https://vjudge.net/contest/68966#problem/N 题目: 最长有序子序列如果a1 <a2 <... <aN,则排序ai的数字序列. 让给定数字序列(a1,a2,...,aN)的子序列为任何序列(ai1,ai2,...,aiK),其中1 <= i1 <i2 <... <iK <= N 例如,序列(1,7,3,5,9,4,8)具有有

Longest Ordered Subsequence POJ - 2533 最长上升子序列dp

题意:最长上升子序列nlogn写法 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int dp[1005]; 7 int a[1005]; 8 int main(){ 9 int n; 10 while(cin>>n){ 11 for(int i=0;i<n;i++){ 12

poj之最长递增子序列

题目:POJ 2533   Longest Ordered Subsequence 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 &l

POJ 1631(最长上升子序列 nlogn).

~~~~ 由题意可知,因为左边是按1~n的顺序递增排列,要想得到不相交组合,左边后面的一定与相应右边后面的相连,如此一来, 就可以发现其实是一道最长上升子序列的题目,要注意的是N<40000,用n^2的算法一定会超时. 题目链接:http://poj.org/problem?id=1631 ~~~~ nlogn的算法在这里补充一下. 最长不下降子序列的O(nlogn)算法分析如下: 设 A[t]表示序列中的第t个数,F[t]表示从1到t这一段中以t结尾的最长上升子序列的长度,初始时设F [t]

poj之最长公共子序列

题目:poj 1458   Common Subsequence Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequenc

poj 2757 : 最长上升子序列(JAVA)

总时间限制:  2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N.比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等.这

POJ 1458 最长公共子序列 LCS

经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1], j, dp[i, j - 1]); 设有字符串X和字符串Y,dp[i, j]表示的是X的前i个字符与Y的前j个字符的最长公共子序列长度. 如果X[i] == Y[j] ,那么这个字符与之前的LCS 一定可以构成一个新的LCS: 如果X[i] != Y[j] ,则分别考察 dp[i  -1][j

POJ 2250(最长公共子序列 变形)

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa