最长上升子序列

来源:互联网 发布:海南大学网络登录入口 编辑:程序博客网 时间:2024/05/29 18:49

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