您的位置:首页 > 其它

最长递增子序列问题

2015-12-03 18:03 302 查看

最长递增子序列
Time Limit: 1000 MS
Memory Limit: 32768 K
Total Submit: 287(97 users)
Total Accepted: 122(88 users)
Rating:
Special Judge: No
Description
给出一个数字序列求其最长的递增子序列例如序列(1,7,3,5,9,4,8).
(1,7)和(3,4,8)是其递增子序列但其最长的递增子序列是(1,3,5,8)。

Input
本题有多组测试数据,对于每组测试数据第一行是一个整数n(n<=100)代表序列长度。
第二行是n个整数。
Output
最长递增子序列长度
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
#include<stdio.h>

#include<string.h>

int a[110];

int dp[110];

int main()

{

int n;

while(~scanf("%d",&n))

{

memset(dp,0,sizeof(dp));

for(int i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

int ans=1;

dp[0]=1;

for(int i=1;i<n;i++)

{

int max=0;

for(int j=0;j<n;j++)

{

if(a[i]>a[j]&&max<dp[j])max=dp[j];

}

dp[i]=max+1;//无论有没有大于的内个步骤 都要更新dp的值.

if(dp[i]>=ans)ans=dp[i];

}

printf("%d\n",ans);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: