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 }
    蒹葭苍苍,白露为霜; 所谓伊人,在水一方。
  • 相关阅读:
    FreeRTOS 移植到WIN10
    Keil debug command SAVE 命令保存文件的解析
    VS2017 编译 Visual Leak Detector + VLD 使用示例
    LaTeX 中插入GIF图片
    VS2017 + Qt5 + OpenCV400 环境配置
    记一次C++编程引用obj文件作为静态库文件
    Qt 多语言支持
    vscode 解决符号无法识别的问题
    带FIFO的UART数据接收
    MySQL Connector/Python 接口 (三)
  • 原文地址:https://www.cnblogs.com/huwt/p/9980263.html
Copyright © 2011-2022 走看看