首页 > 学院 > 开发设计 > 正文

动态规划 LIS

2019-11-06 06:58:19
字体:
来源:转载
供稿:网友

求连续最长子序列

#include <iostream>using namespace std;const int maxn = 1000+10;int count[maxn],arr[maxn];int main() { int n,ans = 0; cin >> n;//原序列的长度 for(int i = 0;i < n;i++) { count[i] = 1;//将到达计数数组置1,相当于他们本身的序列 cin >> arr[i];//读入要求子序列的数组数据 } for(int i = 2;i < n;i++) { int max = count[0]; for(int j = 0;j <= i - 1;j++) { if(arr[j] < arr[i] && count[j] > max) { max = count[j];//两个条件:首先满足上升,其次是求最长,即if判断中的判断 } count[i] = max + 1;//加上本身,构成连续上升子序列 } if(ans < count[i]) ans = count[i]; } cout << ans << endl; return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表