动态规划 LIS

来源:互联网 发布:武汉软件行业协会 编辑:程序博客网 时间:2024/06/06 04:30

求连续最长子序列

#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;} 
0 0
原创粉丝点击