当前位置:编程学习 > C/C++ >>

POJ 2533 Longest Ordered Subsequence

Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25229 Accepted: 10957
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
最长上升子序列
[cpp]  
#include <stdio.h>  
#include <math.h>  
#include <string.h>  
int a[1100],n;  
int main()  
{  
    int find();  
    int i,j,m,s,t,max;  
    scanf("%d",&n);  
    for(i=0;i<=n-1;i++)  
    {  
        scanf("%d",&a[i]);  
    }  
    max=find();  
    printf("%d\n",max);  
    return 0;  
}  
int find()  
{  
    int i,j,dp[1100],max;  
    memset(dp,0,sizeof(dp));  
    dp[0]=1;  
    for(i=1;i<=n-1;i++)  
    {  
        max=0;  
        for(j=0;j<=i-1;j++)  
        {  
            if(a[i]>a[j])  
            {  
                if(dp[j]>max)  
                {  
                    max=dp[j];  
                }  
            }  
        }  
        dp[i]=max+1;  
    }  
    for(i=0,max=0;i<=n-1;i++)  
    {  
        if(dp[i]>max)  
        {  
            max=dp[i];  
        }  
    }  
    return max;  
}  
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,