POJ2533-Longest Ordered Subsequence

描述:

  A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence (ai1ai2,..., 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.

  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 file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

代码:

  求最长递增子序列的长度。从后向前动规,找当前数之后的大于当前数且动规值最大的值(即当前数能够连接成的最长序列的长度),动规值+1赋给当前值。充分体现了动态规划解决冗余的特点。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 1005

int main(){
    int n,temp,dp[N],seq[N],count,flag,max;
    scanf("%d",&n);
    count=0;
    for( int i=0;i<n;i++ )
        scanf("%d",&seq[i]);
    max=0;
    for( int i=n-1;i>=0;i-- ){
        dp[i]=1;
        for( int j=i+1;j<n;j++ ){
            if( seq[j]>seq[i] && dp[i]<dp[j]+1 )
                dp[i]=dp[j]+1;
        }
        max=(max<dp[i])?dp[i]:max;
    }
    printf("%d\n",max);
    return 0;
}
时间: 2024-11-16 05:52:38

POJ2533-Longest Ordered Subsequence的相关文章

POJ2533——Longest Ordered Subsequence(简单的DP)

Longest Ordered Subsequence DescriptionA 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

POJ-2533 Longest Ordered Subsequence ( DP )

题目链接: 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), where 1 <= i1 < i2 < ..

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 Longest Ordered Subsequence 【最长递增子序列】

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Accepted: 14093 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(动态规划)

Time limit2000 ms Memory limit65536 kB 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 < ... < i

POJ-2533.Longest Ordered Subsequence (LIS模版题)

本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 1e3 + 5; 7 int a[maxn], dp[m

LIS模板题(Longest Ordered Subsequence)

LIS模板题(Longest Ordered Subsequence) poj-2533 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序列 Input 第一行包含一个整数n,表示给出序列的元素个数. 第二行包含n个整数,代表这个序列. 1 <= N <= 1000 Output 输出给出序列的最长子序列的长度. Sample Input 7 1 7 3 5 9 4 8 Sample Output 4 #include <std

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)

poj 2533 Longest Ordered Subsequence(LIS)

Description A numeric sequence of ai is ordered ifa1 <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

[2016-04-01][poj][2533][Longest Ordered Subsequence]

时间:2016-04-01 21:35:02 星期五 题目编号:[2016-04-01][poj][2533][Longest Ordered Subsequence] #include <cstdio> #include <algorithm> using namespace std; int dp[1000 + 10],a[1000 + 10]; int main(){ int n; while(~scanf("%d",&n)){ for(int i