复杂度 On^2
Description
给出一个序列,求出这个序列的最长上升子序列。
序列A的上升子序列B定义如下:
B为A的子序列
B为严格递增序列
Input
第一行包含一个整数n,表示给出序列的元素个数。
第二行包含n个整数,代表这个序列。
1 <= N <= 1000
Output
输出给出序列的最长子序列的长度。
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int i,j,n,max;
int a[1005],b[1005]; //b[i]存储当前的最长上升子序列的值
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
b[0]=1; //初始值
for(i=1;i<n;i++)
{
b[i]=1; //当前b[i] 从1开始
for(j=0;j<i;j++)
if(a[i]>a[j]&&b[j]+1>b[i]) //判断条件
b[i]=b[j]+1;
}
max=0;
for(i=0;i<n;i++)
if(b[i]>max) //寻找最大值
max=b[i];
printf("%d\n",max);
return 0;
}
1347

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



