// ch12.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std;
int main()
{
//输入数据
int N;
cin >> N;
vector<int>nums;
for (int i = 0; i<N; i++)
{
int numIn;
cin >> numIn;
nums.push_back(numIn);
}
int result[1001] = { 1 };//result[i]表示序列0-i上的最长序列长度
int maxLen = 1;//维护输出结果maxLen
for (int i = 1; i<N; i++)
{
if (result[i] == 0) //初始化result数组
result[i] = 1;
//更新
for (int j = 0; j<i; j++) //从前向后扫描以[i-1]为尾部的数组,动态更新result[j](j从0到i-1)值
{
if (nums[i]>nums[j]) {
if ((result[j] + 1)>result[i])
result[i] = result[j] + 1;
if (result[j] + 1>maxLen)
maxLen = result[j + 1];
}
}
// for(int j=0;j<N;j++)
// cout<<result[j];
// cout<<endl;
}
cout << "最长上升子序列的长度为:" << maxLen << endl;
return 0;
}
运行结果如下图: