zoukankan      html  css  js  c++  java
  • 最长上升子序列

    描述

    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, the 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 of this sequence 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.
    给出一个长度为N的数字串,找出一个严格上升的数字序列来.

    输入

    The first line of input contains the length of sequence N (1 <= N <= 1000). The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces.

    输出

    Output must contain a single integer - the length of the longest ordered subsequence of the given sequence.

    样例

    输入

    7
    1 7 3 5 9 4 8

    输出

    4
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,a[10001],sum,ans=INT_MIN;
     4 int f[1001];
     5 int main() {
     6     scanf("%d",&n);
     7     for(int i=1; i<=n; i++)
     8         cin>>a[i];
     9     for(int i=1; i<=n; i++) {
    10         int sum=0;
    11         for(int j=1; j<=i; j++) {
    12             if(a[i]>a[j])
    13                 sum=max(sum,f[j]);
    14             f[i]=sum+1;
    15         }
    16     }
    17     for(int i=1;i<=n;i++)
    18     ans=max(ans,f[i]);
    19     printf("%d",ans);
    20     return 0;
    21 }
  • 相关阅读:
    Python3 sorted() 函数
    [Python网络编程]一个简单的TCP时间服务器
    [爬虫]统计豆瓣读书中每个标签下的前两百本书
    [leetcode]39. Combination Sum
    [leetcode]18. 4Sum
    [leetcode DP]72. Edit Distance
    [leetcode DP]120. Triangle
    [leetcode DP]91. Decode Ways
    [leetcode DP]70. Climbing Stairs
    [leetcode DP]64. Minimum Path Sum
  • 原文地址:https://www.cnblogs.com/sbwll/p/13380971.html
Copyright © 2011-2022 走看看