zoukankan      html  css  js  c++  java
  • Longest Ordered Subsequence

    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
     1 //简单动态规划问题
     2 
     3 #include <stdio.h>
     4 
     5 #define MAX 1005
     6 
     7 int main()
     8 {
     9     int n;
    10     int num[MAX];
    11     int maxLen[MAX];
    12     int max = -1;
    13     int i,j;
    14     scanf("%d",&n);
    15     for (i=1; i<=n; i++)
    16       scanf("%d",&num[i]);
    17     for (i=1; i<=n; i++)
    18       maxLen[i] = 1;
    19     for (i=1; i<=n; i++)
    20       for (j=1; j<i; j++)
    21     {
    22       if(num[i]>num[j]&&maxLen[i]<=maxLen[j])
    23         maxLen[i] = maxLen[j]+1;
    24     }
    25     for (i=1; i<=n; i++)
    26       if (maxLen[i]>max)
    27         max = maxLen[i];
    28     printf("%d",max);
    29     return 0;
    30 }
    蒹葭苍苍,白露为霜; 所谓伊人,在水一方。
  • 相关阅读:
    Python-dict与set
    Python-实现对表插入百万条数据
    Python—元组tuple
    数据库查询
    python-操作MySQL数据库
    Python-类的继承
    Python-内置类属性
    Python-类的概念及使用1
    Python异常处理
    了解dto概念,什么是DTO
  • 原文地址:https://www.cnblogs.com/huwt/p/9980263.html
Copyright © 2011-2022 走看看