Description
|
设计一个O(n2)时间的算法,找出由n个数组成的序列的最长单调递增子序列。 | ||
Input
第一行:n,代表要输入的数列的个数 第二行:n个数,数字之间用空格格开 | ||
Output
最长单调递增子序列的长度 | ||
Sample Input
5 1 3 5 2 9 | ||
Sample Output
4 |
#include<iostream>
using namespace std;
#define N 100
int m[N];
int num[N];
int main()
{
int n,i,j,max;
while(cin>>n)
{
for(i=1;i<=n;i++)
cin>>num[i];
m[1]=1;
for(i=2;i<=n;i++)
{
max=0;
for(j=1;j<=i-1;j++)
{
if(m[j]>max&&num[i]>num[j])
{
max=m[j];
m[i]=max+1;
}
}
}
cout<<m[n]<<endl;
}
return 0;
}
该博客介绍了如何使用动态规划算法来寻找一个数列中的单调递增最长子序列,详细阐述了问题描述、输入输出格式,并给出了样例输入和输出。

5069

被折叠的 条评论
为什么被折叠?



