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 }
    蒹葭苍苍,白露为霜; 所谓伊人,在水一方。
  • 相关阅读:
    Linux 让终端走代理的几种方法
    golang 项目框架开发
    tensorflow + python + keras 版本对应关系
    Mac OS X下的ldd工具——otool
    jumpserver的安装
    golang 设置代理
    mac install Docker version 19.03.8
    SpringBoot + Spring Cloud Eureka 服务注册与发现
    SpringBoot + Spring Cloud Consul 服务注册和发现
    前端实现大文件上传
  • 原文地址:https://www.cnblogs.com/huwt/p/9980263.html
Copyright © 2011-2022 走看看