在学习动态规划过程中,下面是我自己实现的LIS,时间复杂度O(n),不知道正确与否,先记下来
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN = 510;
int N;
int LF[MAXN];
int LDp[MAXN] = { 0 };
int main() {
bool changup = false;
int MAXNum = -1;
int MAXIndexes = 0;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> LF[i];
}
LDp[0] = 1;
for (int i = 1; i < N; ++i) {
if (LF[i] >= LF[i - 1]) {
if (LF[i] >= MAXNum && changup == true) {
MAXNum = LF[i];
LDp[i] += LDp[MAXIndexes] + 1;
changup = false;
}
else {
MAXIndexes = i;
LDp[i] = LDp[i - 1] + 1;
}
}
else {
changup = true;
LDp[i] = 1;
}
}
//输出
for (int i = 0; i < N; ++i) {
cout << LDp[i] << " ";
}
cout << endl;
system("PAUSE");
return 0;
}