首页 > 其他 > 详细

最长上升子序列

时间:2020-07-26 20:18:21      阅读:87      评论:0      收藏:0      [点我收藏+]

描述

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 }

 

最长上升子序列

原文:https://www.cnblogs.com/sbwll/p/13380971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!