当前位置: 代码迷 >> 综合 >> 最长上升子序列 oj
  详细解决方案

最长上升子序列 oj

热度:1   发布时间:2024-01-14 22:37:27.0

最长上升子序列

Time Limit: 3000 ms  Memory Limit: 65536 KiB
Submit  Statistic
Problem Description
一个数的序列bi,当b 1 < b 2 < ... < b S的时候,我们称这个序列是上升的。对于给定的一个序列(a 1, a 2, ..., a N),我们可以得到一些上升的子序列(a i1, a i2, ..., a iK),这里1<= i 1 < i 2 < ... < i K <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5, 8)。

你的任务,就是对于给定的序列,求出最长上升子序列的长度。
Input
输入的第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000。
Output
最长上升子序列的长度。
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
Hint
Source
Northeastern Europe 2002
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 2000
int b[N];
int max[N];
int main()
{int n,m,i,j;scanf("%d",&n);for(i = 1;i<=n;i++)scanf("%d",&b[i]);max[1] =1;for(i=2;i<=n;i++){m = 0;for(j=1;j<i;j++){if(b[i] > b[j])if(m<max[j])m=max[j];max[i] = m+1;}}int nmax = -1;for(i = 1;i <= n;i++)if(nmax < max[i])nmax = max[i];printf("%d\n",nmax);return 0;
}

  相关解决方案