美文网首页
四、动态规划(2)--最长上升子序列

四、动态规划(2)--最长上升子序列

作者: 安东可 | 来源:发表于2017-08-10 11:13 被阅读9次
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;


const int MAX_N = 1010;
int a[MAX_N], maxLen[MAX_N];

int main(){
    int N;
    cout << "-------------------------" << endl;
    cout << "输入序列长度:";
    cin >> N;
    cout << "输入序列:" << endl;
    for (int i = 1; i <= N; ++i){
        cin >> a[i];
        maxLen[i] = 1;
    }
    /*
    cout << "-------------------------" << endl;
    // 人人为我递推
    for (int i = 2; i <= N;++i)
        for (int j = 1; j < i; ++j)
        {
            if (a[i]>a[j])
                maxLen[i] = max(maxLen[i], maxLen[j]+1);
        }
        cout <<"最大长度为:"<<* max_element(maxLen + 1, maxLen + N + 1) << endl;
    cout << "-------------------------" << endl;
    */

    //我为人人递推
    for (int i = 1; i <= N;++i)
        for (int j = i + 1; j <= N; ++j)
        {
            if (a[j] > a[i])
                maxLen[j] = max(maxLen[j] ,maxLen[i] + 1);
        }
    cout << "最大长度为:" << *max_element(maxLen + 1, maxLen + N + 1) << endl;
    cout << "-------------------------" << endl;
    return 0;
}

相关文章

网友评论

      本文标题:四、动态规划(2)--最长上升子序列

      本文链接:https://www.haomeiwen.com/subject/wvvdhxtx.html