1576 最长严格上升子序列
题目描述 Description
给一个数组a1, a2 ... an,找到最长的上升降子序列ab1<ab2< .. <abk,其中b1<b2<..bk。
输出长度即可。
输入描述 Input Description
第一行,一个整数N。
第二行 ,N个整数(N < = 5000)
输出描述 Output Description
输出K的极大值,即最长不下降子序列的长度
样例输入 Sample Input
5
9 3 6 2 7
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
【样例解释】
最长不下降子序列为3,6,7
题解:
简单的序列dp!
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char** argv) {
int n;
cin>>n;
int i,j;
int a[5005],dp[5005];
for(i=1;i<=n;i++){
cin>>a[i];
dp[i]=1;
}
int ans=0;
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++){
if(a[j]>a[i]) dp[j]=max(dp[i]+1,dp[j]);
if(dp[j]>ans) ans=dp[j];
}
}
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号